python键盘输入一串字符串统计里面各种字符串类型的个数代码展示
时间: 2024-05-03 09:20:37 浏览: 14
好的,下面是Python代码展示:
```python
str_input = input("请输入一串字符串:") # 获取键盘输入的字符串
count_letters = 0 # 字母个数
count_numbers = 0 # 数字个数
count_spaces = 0 # 空格个数
count_others = 0 # 其它字符个数
for i in str_input:
if i.isalpha(): # 判断是否为字母
count_letters += 1
elif i.isdigit(): # 判断是否为数字
count_numbers += 1
elif i.isspace(): # 判断是否为空格
count_spaces += 1
else:
count_others += 1
print("字母个数:", count_letters)
print("数字个数:", count_numbers)
print("空格个数:", count_spaces)
print("其它字符个数:", count_others)
```
希望能对你有所帮助,如果你还有其他问题,请随时提出。
相关问题
从键盘输入若干字符串,分别统计英文字母空格数字和其他字符的个数
好的,以下是实现代码:
```python
string = input("请输入字符串:")
letters = 0
spaces = 0
digits = 0
others = 0
for char in string:
if char.isalpha():
letters += 1
elif char.isspace():
spaces += 1
elif char.isdigit():
digits += 1
else:
others += 1
print("英文字母个数:", letters)
print("空格个数:", spaces)
print("数字个数:", digits)
print("其他字符个数:", others)
```
输入一个字符串后,程序会遍历字符串中的每个字符,通过 `isalpha()`、`isspace()`、`isdigit()` 判断字符类型并累加个数,最后输出统计结果。
从键盘输入的一串字符串,输出其中不同的字符以及它们各自的个数
你好,非常感谢您使用ChitGPT人工智能进行对话!接下来是回答您的问题:
针对您的问题,可以通过以下Python代码实现:
```python
string = input("请输入一串字符串:")
# 初始化字典,用于存放不同字符以及它们各自的个数
char_dict = {}
# 遍历字符串,统计每个字符的数量
for char in string:
if char in char_dict:
char_dict[char] += 1
else:
char_dict[char] = 1
# 输出结果
print("不同字符及各自数量:")
for char, count in char_dict.items():
print(char, ":", count)
```
请注意,此代码仅适用于英文字符,如果需要考虑中文字符等其他字符类型,则需要进行适当的修改。
希望能够为您解决问题,如有其他问题欢迎再次询问!