判断字符串字母,数字,下划线出现的次数

S = input('input a string:\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(letters,space,digit,others)

猜你喜欢

转载自blog.csdn.net/qwertyuiopasdfgg/article/details/89980406