函数练习题

写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

def check_str(msg):
    res = {
        'num': 0,
        'string': 0,
        'space': 0,
        'other': 0
    }
    for s in msg:
        if s.isdigit():
            res['num'] += 1
        elif s.isalpha():
            res['string'] += 1
        elif s.isspace():
            res['space'] += 1
        else:
            res['other'] += 1

    return res


ret = check_str("hello !name  password:a1w2d3f4g5")
print(ret)

打印结果:
{'num': 5, 'string': 22, 'space': 3, 'other': 2}

  

posted on 2018-12-03 16:37  赫晓蕊  阅读(127)  评论(0编辑  收藏  举报

导航