【Python】输入一行字符(20 个以上字符),分别统计出其中英文字母、空格、数字和其它字 符的个数。

输入一行字符(20 个以上字符),分别统计出其中英文字母、空格、数字和其它字 符的个数。
运行结果如下:
请输入一个字符串:45se r,d5d~ s58*
英文字母=6 个,空格=4 个,数字=5 个,其他=3 个

s=input('请输入一个字符串:\n')
letters=0
space=0
digit=0
others=0
for c in s:
    if c.isalpha():
        letters+=1
    elif c.isspace():
        space+=1
    elif c.isdigit():
        digit+=1
    else:
        others+=1
print('英文字母=%d 个,空格=%d 个,数字=%d 个,其他=%d 个'%(letters,space,digit,others))

在这里插入图片描述

发布了51 篇原创文章 · 获赞 229 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/famur/article/details/105197657