1. 程式人生 > >python字串分類統計

python字串分類統計

使用者從鍵盤輸入一行字元,統計並輸出不同字元的個數。

#字元分類統計
count_alphas=0
count_spaces=0
count_digits=0
count_others=0
s=input("請輸入一個字串:")
for c in s:
    if c.isalpha():
        count_alphas=count_alphas+1
    elif c.isspace():
        count_spaces=count_spaces+1
    elif c.isdigit():
        count_digits=count_digits+1
    else:
        count_others=count_others+1
print("該字串中共有英文字元{}個,空格{}個,數字{}個,其他字元{}個".format(count_alphas,count_spaces,count_digits,count_others))
print("--------END-------")