在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