To judge a word is Chinese character or not in Python

Note: This will take into effect in the case of no spaces in English. That is to say, words only, sentence not.

Example 1

word = 'english'
if word.isalnum():
    print 'only letter and number, no sapce or other character'
    print len(word)
else:
    print 'space or other character inludes'
    print len(word) / 2

Output:

only letter and number, no sapce or other character
7

Example 2

word = '工具'
if word.isalnum():
    print 'only letter and number, no sapce or other character'
    print len(word)
else:
    print 'space or other character inludes'
    print len(word) / 2

Output:

space or other character inludes
2

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

4 × 4 =

33 − = 27