输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。
input_str=input("请输入一行字符: ") letter=0 #表示英文字母的个数 space=0 #表示空格的个数 digit=0 # 表示数字的个数 others=0 #表示其它字符的个数 for char in input_str: if char.isalpha(): #判断字符char是否字母 letter+=1 elif char.isspace(): # 判断是否空格 space+=1 elif char.isdigit(): #判断是否数字 digit+=1 else: others+=1 # 输出结果: print(f"英文字母个数:{letter}") print(f"空格个数:{space}") print(f"数字个数:{digit}") print(f"其它字符个数{others}")