python-统计字符

#输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数
import string

s='ww-m\23 4j)'
letter=0
space=0
digit=0
other=0
for c in s:
    if c.isalpha():
        letter+=1
    elif c.isspace():
        space+=1
    elif c.isdigit():
        digit+=1
    else:
        other+=1
print('The are %d letters,%d spaces,%d digits and %d other characters in your string.'
      %(letter,space,digit,other))

结果:

The are 4 letters,1 spaces,1 digits and 3 other characters in your string.

猜你喜欢

转载自blog.csdn.net/suxiaorui/article/details/84874527