当前位置: 代码迷 >> 综合 >> 编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果。
  详细解决方案

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

热度:71   发布时间:2023-11-28 04:59:21.0

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

def statisic(characters):a = 0b = 0c = 0d = 0       #设置常量来储存各种字符数for i in characters:if i.isdigit():a += 1elif i.islower():b += 1elif i.isupper():c += 1else: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}")
#输出各结果