为什么我的isalpha函数会输出True?我的字符串明明是中文,可函数的输出结果是True。
根据参考链接,对于包含unicode的字符串,isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。如果此区域都是字母,则会返回为True,不一定表示只有26个英文字母;(引用自参考链接)
将字符串使用encode('utf-8')编码一下字符串即可。
测试如下:
参考链接:
# 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())