search

Home  >  Q&A  >  body text

python - 为什么 unicode 的中文字符串,调用 isalpha()或 isalnum()返回的是 True 呢?

例如:

s = u"中国"
print s.isalpha()

输出竟然为True. 这是什么原因?
那么想判断字符串是否只有字母组成出了正则,还有什么好方法?

PHP中文网PHP中文网2869 days ago1556

reply all(1)I'll reply

  • PHP中文网

    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)
    

    reply
    0
  • Cancelreply