输入一个字符串,分别统计出大写字母、小写字母、空格、数字和其他字符的个数,并输出结果
def PythonHuaHua(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
PythonHuaHua(s) # 调用函数
letter = 0
space = 0
num = 0
other = 0
str0 = input('请输入一段字符串:')
for i in str0:
if i.isalpha():
letter += 1
elif i.isspace():
space += 1
elif i.isdigit():
num += 1
else:
other += 1
print('字母个数为:', letter)
print('空格个数为:', space)
print('数字个数为:', num)
print('其他字符个数为:', other)