If the string has an alphabet or a number, return true. Otherwise, return false.开发者_开发知识库
I have to do this, right?
return re.match('[A-Z0-9]',thestring)
Use thestring.isalnum()
method.
>>> '123abc'.isalnum()
True
>>> '123'.isalnum()
True
>>> 'abc'.isalnum()
True
>>> '123#$%abc'.isalnum()
>>> a = '123abc'
>>> (a.isalnum()) and (not a.isalpha()) and (not a.isnumeric())
True
>>>
If you want to check if ALL characters are alphanumeric:
string.isalnum()
(as @DrTyrsa pointed out), orbool(re.match('[a-z0-9]+$', thestring, re.IGNORECASE))
If you want to check if at least one alphanumeric character is present:
import string
alnum = set(string.letters + string.digits)
len(set(thestring) & alnum) > 0
or
bool(re.search('[a-z0-9]', thestring, re.IGNORECASE))
It might be a while, but if you want to figure out if the string at at least 1 alphabet or numeral, we could use
re.match('.*[a-zA-Z0-9].*', yourstring)
What about
stringsample.isalpha()
method?
精彩评论