469,640 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,640 developers. It's quick & easy.

String comparisons

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
8 1400
ilikepython
844 Expert 512MB
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
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
844 Expert 512MB
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
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
6,596 Expert 4TB
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
Expand|Select|Wrap|Line Numbers
  1. >>> from string import letters
  2. >>> "B" in letters
  3. True
  4. >>> 
Okay excellent, thanks.
Aug 20 '07 #7
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> "B".isupper()
  2. True
  3. >>> "b".isupper()
  4. False
  5. >>> 
  6.  
Aug 20 '07 #8
bartonc
6,596 Expert 4TB
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.

Similar topics

6 posts views Thread by Maileen | last post: by
1 post views Thread by Dino Buljubasic | last post: by
4 posts views Thread by Sjaakie | last post: by
4 posts views Thread by Mark Rae | last post: by
2 posts views Thread by Nishad | last post: by
26 posts views Thread by Neville Lang | last post: by
14 posts views Thread by Steve Bergman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.