例如:
s = u"中国"
print s.isalpha()
输出竟然为True. 这是什么原因?
那么想判断字符串是否只有字母组成出了正则,还有什么好方法?
PHP中文网2017-04-17 13:38:10
For unicode string, string.isalpha
will determine whether the characters in the string are all composed of letters based on whether they belong to the LETTER area of Unicode encoding. So the result is True, which does not necessarily mean that there are only 26 English letters.
Regular expressions should be the simplest method.
Of course you can:
def isAlpha(word):
try:
return word.encode('ascii').isalpha()
except UnicodeEncodeError:
return False
s = u"中国"
isAlpha(s)