通过键盘输入一个字符串,统计其中空格,数字,中文字符,英文字符和其他字符的个数(中文字符的判断条件可以使用 s1>='\u4e00' and s1<='\u9fa5')
输出样例:
print(“输入的字符串中有{}个空格,{}个数字”.format())
输入的字符串中有 个空格, 个数字, 个中文, 个英文字符, 个其他字符
输入后,遍历字符串一个个判断即可
word = 0
num = 0
chinese= 0
space = 0
other = 0
a = input()
for s in a:
if s>='\u4e00' and s<='\u9fa5':
chinese += 1
elif s.isalpha():
word += 1
elif s.isdigit():
num += 1
elif s == ' ':
space += 1
else:
other += 1
print('输入的字符串中有{}个空格,{}个数字,{}个中文字符,{}个英文字符,{}个其他字符'.format(space,num,chinese,word,other))
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!