python计算字符个数
Python输入一行字符,分别统计出其中大小写英文字母、空格、数字和其它字符的个数。
import string
def SlowSnail(s):
up = 0
low = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isupper():
up += 1
elif c.islower():
low += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print('大写字母 = %d,小写字母 = %d,空格 = %d,数字 = %d,其他 = %d' % (up, low, space, digit, others))
while 1:
s = input('请输入一个字符串:\n')
if '-1' in s: # 设置退出循环条件
break
SlowSnail(s) # 调用函数