输入一行字符,统计其中的大写、小写、数字、空格和其它字符的个数
时间: 2024-06-13 14:06:35 浏览: 12
以下是Python代码实现:
```python
s = input("请输入一行字符:")
d = {'upper': 0, 'lower': 0, 'digit': 0, 'space': 0, 'other': 0}
for c in s:
if c.isupper():
d['upper'] += 1
elif c.islower():
d['lower'] += 1
elif c.isdigit():
d['digit'] += 1
elif c.isspace():
d['space'] += 1
else:
d['other'] += 1
print("大写字母个数:", d['upper'])
print("小写字母个数:", d['lower'])
print("数字个数:", d['digit'])
print("空格个数:", d['space'])
print("其他字符个数:", d['other'])
```
输入一行字符后,程序会统计其中的大写字母、小写字母、数字、空格和其他字符的个数并输出。
--相关问题--:
1. 如何判断一个字符是不是字母?
2. 如何判断一个字符是不是数字?
3. 如何判断一个字符是不是空格?
相关问题
输入一行字符回车结束编程统计其中大写字母小写字母数字字符空格和其他字符的个数
```python
string = input("请输入一行字符:")
upper_count = 0
lower_count = 0
digit_count = 0
space_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
elif char.isspace():
space_count += 1
else:
other_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("空格个数:", space_count)
print("其他字符个数:", other_count)
```
示例输出:
```
请输入一行字符:Hello, World! 123
大写字母个数: 2
小写字母个数: 8
数字个数: 3
空格个数: 2
其他字符个数: 2
```
输入一行字符,分别统计出其中的英文字母大写小写、空格、数字和其它字符的个数。
以下是Python的一个例子,可以实现输入一行字符,分别统计出其中的英文字母大写小写、空格、数字和其它字符的个数:
```python
line = input("请输入一行字符:")
upper = 0
lower = 0
digit = 0
space = 0
other = 0
for i in line:
if i.isupper():
upper += 1
elif i.islower():
lower += 1
elif i.isdigit():
digit += 1
elif i.isspace():
space += 1
else:
other += 1
print("大写字母个数:", upper)
print("小写字母个数:", lower)
print("数字个数:", digit)
print("空格个数:", space)
print("其他字符个数:", other)
```