编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果
时间: 2023-12-12 07:13:04 浏览: 23
统计一个字符串中某个字符的个数
4星 · 用户满意度95%
def count_chars(s):
upper = 0
lower = 0
digit = 0
other = 0
for char in s:
if char.isupper():
upper += 1
elif char.islower():
lower += 1
elif char.isdigit():
digit += 1
else:
other += 1
return (upper, lower, digit, other)
s = "Hello, World! 123"
print(count_chars(s)) # (2, 8, 3, 3)
阅读全文