输入一行字符分别统计
时间: 2023-07-03 22:22:05 浏览: 84
好的,以下是 Python 代码示例:
```python
s = input("请输入一行字符:")
letters = 0
digits = 0
spaces = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isdigit():
digits += 1
elif c.isspace():
spaces += 1
else:
others += 1
print("字母个数:", letters)
print("数字个数:", digits)
print("空格个数:", spaces)
print("其他字符个数:", others)
```
这段代码会让你输入一行字符,然后统计其中字母、数字、空格和其他字符的个数,并打印出结果。其中,我们使用了字符串的 `isalpha()`、`isdigit()` 和 `isspace()` 方法对每个字符进行分类判断。
相关问题
本题要求编写程序,输入一行字符,统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。\n\n输入格式:\n输入在一行中给出若干字符,最后一个回车表示输入结束,不算在内。\n\n输出格式:\n在一
这道题要求你编写一个程序,输入一行字符,统计其中数字字符、空格和其他字符的个数。建议使用switch语句编写。输入格式为输入在一行中给出若干字符,最后一个回车表示输入结束,不算在内。输出格式为在一行中输出3个整数,分别表示数字字符、空格和其他字符的个数。
阅读全文
相关推荐








