python 编程功能:输入自定义的字符串,利用函数计算传入字符串中的数字、字母、空格和其他的个数?

如题所述

str_1 = input("请输入一个字符串:")
num_num = 0
num_alp = 0
num_null = 0
num_else = 0
for s in str_1:
if s >= '0' and s <= '9':
num_num += 1
elif (s >= 'a' and s <= 'z') or (s >= 'A' and s <= 'Z'):
num_alp += 1
elif s == ' ':
num_null += 1
else:
num_else += 1
print("数字的个数:",num_num)
print("字母的个数:",num_alp)
print("空格的个数:",num_null)
print("其他的个数:",num_else)

运行结果:
请输入一个字符串:haha 123 ** 11
数字的个数: 5
字母的个数: 4
空格的个数: 3
其他的个数: 2

望采纳!
温馨提示:答案为网友推荐,仅供参考
相似回答