By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

String comparisons

P: 60
I am making a program that requires me to test whether a certain character in a string is capital. I was wondering if there was anyway I could do this like:

string is A-Z

That would determine whether the string is any letter from A to Z
Aug 20 '07 #1
Share this Question
Share on Google+
8 Replies


ilikepython
Expert 100+
P: 844
I am making a program that requires me to test whether a certain character in a string is capital. I was wondering if there was anyway I could do this like:

string is A-Z

That would determine whether the string is any letter from A to Z
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> 'g' in string.lowercase
  3. True
  4. >>> 'D' in string.lowercase
  5. False
  6.  
Aug 20 '07 #2

P: 60
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> 'g' in string.lowercase
  3. True
  4. >>> 'D' in string.lowercase
  5. False
  6.  
Okay thanks, but is there a way to determine whether a letter is a vowel or not?
kinda like string=['a','e','i','o','u']
Aug 20 '07 #3

ilikepython
Expert 100+
P: 844
Okay thanks, but is there a way to determine whether a letter is a vowel or not?
kinda like string=['a','e','i','o','u']
Well...
Expand|Select|Wrap|Line Numbers
  1. >>> vowels = "aeiou"
  2. >>> 'e' in vowels
  3. True
  4. >>> 'f' in vowels
  5. False
  6.  
Aug 20 '07 #4

P: 60
Well...
Expand|Select|Wrap|Line Numbers
  1. >>> vowels = "aeiou"
  2. >>> 'e' in vowels
  3. True
  4. >>> 'f' in vowels
  5. False
  6.  
Okay that makes sense now. But is there anyway to make it like a to z without typing each letter?
Aug 20 '07 #5

bartonc
Expert 5K+
P: 6,596
Okay that makes sense now. But is there anyway to make it like a to z without typing each letter?
Expand|Select|Wrap|Line Numbers
  1. >>> from string import letters
  2. >>> "B" in letters
  3. True
  4. >>> 
Aug 20 '07 #6

P: 60
Expand|Select|Wrap|Line Numbers
  1. >>> from string import letters
  2. >>> "B" in letters
  3. True
  4. >>> 
Okay excellent, thanks.
Aug 20 '07 #7

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. >>> "B".isupper()
  2. True
  3. >>> "b".isupper()
  4. False
  5. >>> 
  6.  
Aug 20 '07 #8

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. >>> "B".isupper()
  2. True
  3. >>> "b".isupper()
  4. False
  5. >>> 
  6.  
Thanks, GD. Strings do have quite a few handy methods hung on them.
I also meant to point out that the string module is useful for the constants defined there, but the functions which are now string methods should not be used.
Aug 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.