例7.用户从键盘输入一行字符,编写一个程序,统计并输出其中的英文字符(包括中文字符)、数字、空格和其他字符个数。
1 #字符数统计.py 2 Str = input('请输入一行字符:') 3 alpha = 0 4 space = 0 5 num = 0 6 other = 0 7 for i in Str: 8 if i.isalpha(): 9 alpha += 1 10 elif i.isspace(): 11 space += 1 12 elif i.isnumeric(): 13 num += 1 14 else : 15 other += 1 16 print('英文字符数{},空格字符数{},数字字符数{},其他字符数{}' 17 .format(alpha,space,num,other))