python 用if 判断输入内容是不是汉字
可以使用Python内置模块unicodedata中的函数来判断一个字符是否为汉字。具体实现如下:
import unicodedata
def is_chinese(word):
for char in word:
if 'CJK' in unicodedata.name(char):
return True
return False
# 测试
word = input('请输入一个字符:')
if is_chinese(word):
print('是汉字')
else:
print('不是汉字')
在这个代码中,我们通过unicodedata.name()函数获取字符的Unicode名称,并判断是否包含字符串"CJK",如果包含,则表示该字符为汉字。
原文地址: https://www.cveoy.top/t/topic/huMy 著作权归作者所有。请勿转载和采集!