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

Posted sunnybowen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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))

技术分享图片

 

以上是关于Python(66)_判断用户传入的参数str中计算数字,字母,空格,以及其他的个数,并返回结果的主要内容,如果未能解决你的问题,请参考以下文章