How To Check For [a-z] In String?

Christopher Koppler klapotec at chello.at
Thu Oct 2 13:31:18 EDT 2003


On Thu, 02 Oct 2003 19:01:27 +0200, Peter Otten <__peter__ at web.de>
wrote:

>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

<High embarassement mode>
Big oops. Yeah, that goes to show what happens if you multitask
yourself and don't let your computer do it - I'm preparing for the
second of the two Oracle Certified Associate exams, which I take
tomorrow, and thought I'd take some time out and maybe answer some
questions here... Seems I'm a bit too confused and disorganized to do
that right now...
</HEM>

--
Christopher




More information about the Python-list mailing list