有一行文字,50个字符,要求分别统计出其中的英文大写字母、小写字母、数字、空格及其他字符的个数。
时间: 2024-05-05 08:20:03 浏览: 13
好的,这是一个很好的问题!我可以为您提供一个 Python 的代码示例,以便您完成此任务。代码如下:
```python
text = "This is a sample text with 3 numbers: 123 and some symbols!$%#"
upper_count = 0
lower_count = 0
digit_count = 0
space_count = 0
other_count = 0
for char in text:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
elif char.isspace():
space_count += 1
else:
other_count += 1
print("Upper case letters:", upper_count)
print("Lower case letters:", lower_count)
print("Digits:", digit_count)
print("Spaces:", space_count)
print("Other characters:", other_count)
```
您可以将此代码复制并粘贴到 Python 编辑器中运行,也可以将其保存为 `.py` 文件进行运行。我希望这可以回答您的问题!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)