isalpha输出错误

img

为什么我的isalpha函数会输出True?我的字符串明明是中文,可函数的输出结果是True。

根据参考链接,对于包含unicode的字符串,isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。如果此区域都是字母,则会返回为True,不一定表示只有26个英文字母;(引用自参考链接)

将字符串使用encode('utf-8')编码一下字符串即可。

测试如下:

参考链接:


Python中调用isalpha()方法判断中文字符串为true_月白风清好过河的博客-CSDN博客 可以用encode(‘utf-8’)解决mystr_chinese = '你好'print(mystr_chinese.isalpha()) *结果为Trueprint(mystr_chinese.encode('utf-8').isalpha()) *结果为False其实不仅是中文字符串,其他语言如日文也有这种现象mystr_japanese = 'こんにちは'print(my... https://blog.csdn.net/weixin_42190224/article/details/89532325

Python encode方法:XX.encode(‘utf-8‘)究竟是什么?_encode(utf-8)_程序米虫的博客-CSDN博客 Python encode方法:XX.encode(‘utf-8’)究竟是什么?在Python学习中,我们经常会碰到这些内容:S.encode([encoding='utf-8'][,errors='strict'])这时,我们会想等会去看看这究竟是什么?然后,就没有等会了。。。当然,也有人会觉得记就完事了。但不管怎样,我在这里简单讲一下它,希望帮助到大家。一.描述encode() 方法以指定的编码格式编码字符串,默认编码为 ‘utf-8’。对应的解码方法:bytes decode() _encode(utf-8) https://blog.csdn.net/qq_49551075/article/details/107556997



# https://blog.csdn.net/qq_41655933/article/details/104335334
# https://blog.csdn.net/weixin_42190224/article/details/89532325
print('辣'.isalpha())

s = '辣'
#s = '辣ab'
#for ch in s:
 #   print(ch)
 
# https://blog.csdn.net/qq_49551075/article/details/107556997
ec = s.encode('utf-8') # 使用encode()函数将字符串以'utf-8'编码
#for ch in ec:
#    print(ch)

print(ec.isalpha())


img