输入一个字符串,分别统计出其中大写字母、小写字母、数字及其他字符的个数。
时间: 2023-12-14 20:02:27 浏览: 27
可以使用Python中的字符串方法和循环来实现这个功能。
代码如下:
```python
string = input("请输入一个字符串:")
upper_count = 0 # 大写字母的个数
lower_count = 0 # 小写字母的个数
digit_count = 0 # 数字的个数
other_count = 0 # 其他字符的个数
for char in string:
if char.isupper(): # 判断字符是否为大写字母
upper_count += 1
elif char.islower(): # 判断字符是否为小写字母
lower_count += 1
elif char.isdigit(): # 判断字符是否为数字
digit_count += 1
else:
other_count += 1 # 其他字符
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
```
例如,输入字符串 "Hello, World! 123",输出结果为:
```
大写字母个数: 2
小写字母个数: 8
数字个数: 3
其他字符个数: 4
```
相关问题
编写程序输入字符串分别统计其中大写字母小写字母和数字字符的个数
好的,以下是Python的代码实现:
```python
s = input("请输入字符串:")
upper_count = 0 # 大写字母计数器
lower_count = 0 # 小写字母计数器
digit_count = 0 # 数字计数器
for c in s:
if c.isupper(): # 判断是否为大写字母
upper_count += 1
elif c.islower(): # 判断是否为小写字母
lower_count += 1
elif c.isdigit(): # 判断是否为数字
digit_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
```
运行程序后,输入任意字符串,即可得到该字符串中大写字母、小写字母和数字字符的个数。
键盘输入一个字符串,统计其中的大写字母、小写字母、数字字母、其他字符的个数。
可以使用Python语言实现这个功能,代码如下:
```python
# 键盘输入一个字符串
s = input("请输入一个字符串:")
# 初始化各类字符的个数
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
# 输出各类字符的个数
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
```
运行程序后,输入一个字符串,程序会自动统计其中大写字母、小写字母、数字、其他字符的个数并输出。