用Python编写函数,接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并以元组的形式返回结果
时间: 2024-05-05 08:17:14 浏览: 30
python 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数 (示例)
5星 · 资源好评率100%
```python
def count_chars(s):
upper_count = 0
lower_count = 0
digit_count = 0
other_count = 0
for c in s:
if c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
elif c.isdigit():
digit_count += 1
else:
other_count += 1
return (upper_count, lower_count, digit_count, other_count)
```
使用示例:
```python
s = "Hello, World! 123"
result = count_chars(s)
print(result) # 输出 (2, 8, 3, 4)
```
阅读全文