用python3判断一个字符串 包含 中文

时间:2023-03-10 05:40:37
用python3判断一个字符串 包含 中文

在python中一个汉字算一个字符,一个英文字母算一个字符

用 ord() 函数判断单个字符的unicode编码是否大于255即可。

s = '我xx们的88工作和生rr活168'
n = 0
for c in s:
if ord(c) > 255:
print(c)

  

一般来说,中文常用字的范围是:[\u4e00-\u9fa5]

准确点判断中文字符,可以这样比较:

a = "你好"
b = "</p>你好"
c = 'asdf'
def isAllZh(s):
'包含汉字的返回TRUE'
for c in s:
if '\u4e00' <= c <= '\u9fa5':
return True
return False print(isAllZh(a))
print(isAllZh(b))
print(isAllZh(c))

True
True
False