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

 

你可能感兴趣的:(python入门)