python统计输出数字,大写字母,小写字母及其他字符个数
时间: 2024-02-03 09:44:34 浏览: 57
可以使用Python中的isnumeric(), isupper()和islower()函数来识别数字、大写字母和小写字母,然后统计它们的个数。以下是一个简单的示例代码:
```python
string = "Hello, World! 123"
num_count = 0
upper_count = 0
lower_count = 0
other_count = 0
for char in string:
if char.isnumeric():
num_count += 1
elif char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
else:
other_count += 1
print("Number of numeric characters:", num_count)
print("Number of uppercase characters:", upper_count)
print("Number of lowercase characters:", lower_count)
print("Number of other characters:", other_count)
```
输出:
```
Number of numeric characters: 3
Number of uppercase characters: 2
Number of lowercase characters: 8
Number of other characters: 7
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)