定义一个函数, 统计字符串中有几个字母,几个数字,几个空格,几个其他字符,并返回结果
def Judge():
count1=0
count2=0
count3=0
count4 = 0
print("请输入一个字符串:")
str = input()
for i in range(len(str)):
if str[i].isalpha() == True:
count1+=1
elif str[i].isdigit() == True:
count2+=1
elif str[i].isspace() == True:
count3+=1
else:
count4+=1
print("字符串有{}个字母,{}个数字,{}个空格,{}个其他字符".format(count1,count2,count3,count4))
Judge()
此函数中只需求出字符串长度,然后遍历字符串挨个判断即可