a ='1234 abcA BCDE_'统计数字,字母,大写字母,小写字母,空格,下划线个数并输出的python代码
时间: 2023-10-07 19:03:56 浏览: 98
python统计字母、空格、数字等字符个数的实例
5星 · 资源好评率100%
```
a = '1234 abcA BCDE_'
digit_count = 0
lower_count = 0
upper_count = 0
letter_count = 0
space_count = 0
underscore_count = 0
for char in a:
if char.isdigit():
digit_count += 1
elif char.islower():
lower_count += 1
letter_count += 1
elif char.isupper():
upper_count += 1
letter_count += 1
elif char.isspace():
space_count += 1
elif char == '_':
underscore_count += 1
print("数字个数:", digit_count)
print("小写字母个数:", lower_count)
print("大写字母个数:", upper_count)
print("字母个数:", letter_count)
print("空格个数:", space_count)
print("下划线个数:", underscore_count)
```
输出结果:
```
数字个数: 4
小写字母个数: 5
大写字母个数: 4
字母个数: 9
空格个数: 2
下划线个数: 1
```
阅读全文