How To Check For [a-z] In String?
Peter Otten
__peter__ at web.de
Thu Oct 2 13:01:27 EDT 2003
Christopher Koppler wrote:
>>Also, how would I check if [a-z] is not in a string?
>
> import string
>
> sometext = "whatever"
> if not string.lowercase in sometext:
> <do what you wanted to do>
Let's see:
>>> import string
>>> sometext = "whatever"
>>> if not string.lowercase in sometext:
... print "do what you wanted to"
...
do what you wanted to
I doubt that this is what you expected.
s1 in s2
tests if s1 is a substring of s2, but you want
def contains(s, chars):
for c in s:
if c in chars:
return True
return False
> expressions, but the string module is your friend in this case, and
> makes for very readable code, which is also unicode-ready, since
> string.(lower|upper)case also contain accented and other characters.
> Just do a dir(string) to see what other goodies there are...
Most of these are already available as methods of the str class and are
duplicated here only for backwards compatibility.
What's actually in string.lowercase/uppercase depends on the locale, you
should by no means take latin-1 for granted.
You have already withdrawn the unicode-ready claim.
Nasty, nasty, nasty :-)
Peter
More information about the Python-list
mailing list