字符串统计个数

输入一行非中文字符,分别统计出其中英文字母、空格、数字和其他字符的个数

def countzimu():
    dict = {}
    shuzi = 0
    zimu = 0
    space = 0
    other = 0
    str = input("请输入任意字符:")
    for i in str:
        if i.isdigit():
            shuzi+=1
        elif i.isalpha():
            zimu+=1
        elif i.isspace():
            space+=1
        else:
            other+=1
    dict['shuzi'] = shuzi
    dict['zimu'] = zimu
    dict['space'] = space
    dict['other'] = other
    return dict
print(countzimu())

  

原文地址:https://www.cnblogs.com/ljf520hj/p/15591538.html