python 键盘输入任意一个字符串,分别输出其中所含英文字母、数字、空格以及其它字符的个数。
时间: 2024-05-11 12:19:00 浏览: 12
可以使用 Python 中的 isdigit(),isalpha() 和 isspace() 函数来判断字符是否为数字、字母和空格。代码如下:
```python
string = input("请输入一个字符串:")
num_count = 0
alpha_count = 0
space_count = 0
other_count = 0
for char in string:
if char.isdigit():
num_count += 1
elif char.isalpha():
alpha_count += 1
elif char.isspace():
space_count += 1
else:
other_count += 1
print("数字个数:", num_count)
print("字母个数:", alpha_count)
print("空格个数:", space_count)
print("其他字符个数:", other_count)
```
在运行时,输入一个字符串,程序会输出字符串中包含数字、字母、空格以及其他字符的个数。
相关问题
python中输入一行字符串,分别统计其中英文字母 空格 数字和其它字符的个数
好的,下面是 Python 代码实现这个功能:
```python
s = input("请输入一行字符串:") # 获取用户输入的字符串
count_letter = 0 # 统计英文字母的个数
count_space = 0 # 统计空格的个数
count_digit = 0 # 统计数字的个数
count_other = 0 # 统计其他字符的个数
for c in s: # 遍历字符串中的每个字符
if c.isalpha(): # 如果是英文字母
count_letter += 1
elif c.isspace(): # 如果是空格
count_space += 1
elif c.isdigit(): # 如果是数字
count_digit += 1
else: # 否则是其他字符
count_other += 1
print("英文字母个数:", count_letter)
print("空格个数:", count_space)
print("数字个数:", count_digit)
print("其他字符个数:", count_other)
```
你可以直接复制这段代码到 Python 环境中运行。输入一行字符串,程序会统计其中英文字母、空格、数字和其他字符的个数,并输出结果。
输入一个字符串,分别统计其中英文字母、数字、空格以及其他字符的个数
好的,下面是 Python 代码实现:
```python
string = input("请输入字符串:")
letters = 0
digits = 0
spaces = 0
others = 0
for char in string:
if char.isalpha():
letters += 1
elif char.isdigit():
digits += 1
elif char.isspace():
spaces += 1
else:
others += 1
print("字符串中英文字母的个数为:", letters)
print("字符串中数字的个数为:", digits)
print("字符串中空格的个数为:", spaces)
print("字符串中其他字符的个数为:", others)
```
示例输入:
```
请输入字符串:Hello 123 World!@
```
示例输出:
```
字符串中英文字母的个数为: 10
字符串中数字的个数为: 3
字符串中空格的个数为: 2
字符串中其他字符的个数为: 2
```