Python(66)_判断用户传入的参数str中计算数字,字母,空格,以及其他的个数,并返回结果

#-*-coding:utf-8-*-
'''
写函数,判断用户传入的参数str中计算数字,字母,空格,以及其他的个数,并返回结果
'''
content = input('>>>')

def func(s):
    num = 0
    alpha = 0
    space = 0
    others = 0
    dic ={
        'num':0,
        'alpha':0,
        'space':0,
        'other':0
    }
    for i in s:
        if i.isdigit():
            dic['num'] +=1
        elif i.isalpha():
            dic['alpha'] +=1
        elif i.isspace():
            dic['space'] +=1
        else:
            dic['other'] +=1
    return dic
print(func(content))

猜你喜欢

转载自www.cnblogs.com/sunnybowen/p/10257653.html