编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。

编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。

def statisic(characters):
    a = 0
    b = 0
    c = 0
    d = 0       #设置常量来储存各种字符数
    for i in characters:
        if i.isdigit():
            a += 1
        elif i.islower():
            b += 1
        elif i.isupper():
            c += 1
        else:
            d += 1
                        #用循环来分别统计字符种类

    return a, b, c, d   #返回统计数

mes = "\nPlease enter some characters ,"
mes += " and I will help you statisic it."
print(mes)      #给予提示
characters = input("the characters is: \n")
#接受字符

numbers,lower,upper,others = statisic(characters)#对接收到的字符进行统计

result = (numbers,lower,upper,others)#化为元组
print("结果是: ")
print(f"\t数字有{numbers}个。")
print(f"\t大写字母有{upper}个。")
print(f"\t小写字母有{lower}个。")
print(f"\t其他字符有有{others}个。")
print(f"元组为{result}")
#输出各结果

  • 13
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值