python实现字符串元素计数
代码:
str = input('请输入字符串:\n')
letter = 0
space = 0
number = 0
symbol = 0
underLine = 0
for char in str:
if 'a' <= char <= 'z' or 'A' <= char <= 'Z':
letter += 1
elif '0' <= char <= '9':
number += 1
elif char == ' ':
space += 1
elif char == '_':
underLine += 1
else:
symbol += 1
print('字母有{}个'.format(letter), '数字有{}个'.format(number),
'下划线有{}个'.format(underLine), '符号有{}个'.format(symbol), '空格有{}个'.format(space))