用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