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

Christopher Koppler klapotec at chello.at
Thu Oct 2 12:13:59 EDT 2003


On Thu, 02 Oct 2003 14:56:10 GMT, Hank Kingwood
<hank at bogusaddress.xyz> 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>

>
>Thanks again,
>Hank
>
>Hank Kingwood wrote:
>> This is probably an easy question, but I can't find a function (maybe my 
>> syntax is off...) to search for [a-z] in a string.  If someone would 
>> help out, I'd appreciate it!
>> 
>> Also, how would you recommend searching for the following in the same 
>> string:
>>   [a-z]

if string.lowercase in sometext:

>>   [A-Z]

if string.uppercase in sometext: ...

>>   [0-9]

if string.digits in sometext: ...

>>   -
>> 
>> My approach would be to perform four separte checks, but I'm thinking 
>> there might be some cool approach that would use a dictionary or array. 
>>  Ideas?
>> 
>> Thanks,
>> Hank
>> 

As others have shown, you can of course do this with regular
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...

--
Christopher




More information about the Python-list mailing list