題目描述:
輸入一行字符,輸出其中空格、數字、中文字符、英文字符和其他字符的個數。
實現代碼:
str=input("請輸入一行字符:\n")
chinese=0
letters=0
space=0
digit=0
others=0
for c in str:
if c.isspace():
space+=1
elif c.isdigit():
digit+=1
elif c>=u'\u4e00' and c<=u'\u9fa5':#判斷是否爲中文字符
chinese+=1
elif c.isalpha():
letters += 1
else:
others+=1
print('空格=%d,數字=%d,中文字符=%d,英文字符=%d,其他字符=%d'%(space,digit,chinese,letters,others))