1. 程式人生 > >Python中判斷字串是不是漢字

Python中判斷字串是不是漢字

isalpha()是Python中用來判斷是否為字母的函式,但是當字元為漢字時函式依然返回YES,在開發中可能遇到判斷一個字元是否是漢字的問題,我們可以根據ASCII判斷字元是否為漢字

def isChineseWord(string):
    if string.isalpha():
        if ord(string) in range(65,91) or ord(string) in range(97,123) :
            print("是字母")
            return  False
        else:
            print("是漢字")
            return True
    print("不是漢字也不是字母")
    return False