例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))