Python 中输入一个姓名判断是不是汉字
可以使用 Python 的内置模块 re 来进行正则表达式匹配判断是否为汉字。
代码如下:
import re
name = input("请输入姓名:")
# 正则表达式匹配汉字
pattern = re.compile(r'^[\u4e00-\u9fa5]+$')
match = pattern.match(name)
if match:
print("输入的姓名为汉字")
else:
print("输入的姓名不是汉字")
运行结果:
请输入姓名:张三
输入的姓名为汉字
请输入姓名:Tom
输入的姓名不是汉字
原文地址: https://www.cveoy.top/t/topic/huOu 著作权归作者所有。请勿转载和采集!