3、用python输入一个字符串,判断字符串中有多少个字母?多少个数字?多少个其他符号?
时间: 2023-11-27 15:54:59 浏览: 42
可以使用 Python 中的 isalpha() 和 isdigit() 函数来判断字符串中的字母和数字,其他符号则可以通过判断字符是否既不是字母也不是数字来得到。具体实现如下:
```
string = input("请输入一个字符串:")
alpha_count = 0 # 统计字母个数
digit_count = 0 # 统计数字个数
other_count = 0 # 统计其他符号个数
for char in string:
if char.isalpha():
alpha_count += 1
elif char.isdigit():
digit_count += 1
else:
other_count += 1
print("字母个数为:", alpha_count)
print("数字个数为:", digit_count)
print("其他符号个数为:", other_count)
```
运行程序后,输入一个字符串,程序会统计其中的字母、数字和其他符号的数量并输出。