Day010 编写函数,分别统计各种字符的个数,并以元组的形式返回结果

mac2025-08-29  9

需求:

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

代码:

list_upstr = [] # 大写字母 list_lowstr = [] # 小写字母 list_int = [] # 数字 list_otherstr = [] # 其他字符 def Statistics_str(str0): for i in str0: if i.isupper(): list_upstr.append(i) elif i.islower(): list_lowstr.append(i) elif i.isdigit(): list_int.append(i) else: list_otherstr.append(i) return list_upstr, list_lowstr,list_int, list_otherstr s = input("请输入一串字符:") a, b, c, d = Statistics_str(s) print("大写字母个数:{0},小写字母个数:{1},数字个数:{2},其它字符个数{3}".format(len(a), len(b), len(c), len(d))) a = tuple(a) b = tuple(b) c = tuple(c) d = tuple(d) print(a, b, c, d)

最新回复(0)