python统计字符串中文字符的个数

Unicode编码
Unicode特殊字符编码
\u0020 空格
\u0040 ~ \u005A  大写字母A~Z
\u0061 ~ \u007A  小写字母a~z
\u4E00 ~ \u9FFF  中文字符
\u0030 ~ \u0039   数字

string = input()
num = 0
ch = 0
space = 0
other = 0
for x in string:
    # if x.isnumeric():
    if  '\u0030'<=x<='\u0039':
        num+=1
    # elif x=='\u0020'
    elif x.isspace():
        space+=1
    elif x.isalpha() or '\u4e00' <= x <= '\u9fff':
        ch+=1
    else:
        other+=1
print(ch,space,num,other)

你可能感兴趣的:(python)