用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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM