Python输入一行字符,分别统计出其中大小写英文字母、空格、数字和其它字符的个数。

import string


def SlowSnail(s):
    up = 0
    low = 0
    space = 0
    digit = 0
    others = 0
    for c in s:
        if c.isupper():
            up += 1
        elif c.islower():
            low += 1
        elif c.isspace():
            space += 1
        elif c.isdigit():
            digit += 1
        else:
            others += 1
    print('大写字母 = %d,小写字母 = %d,空格 = %d,数字 = %d,其他 = %d' % (up, low, space, digit, others))


while 1:
    s = input('请输入一个字符串:\n')
    if '-1' in s:  # 设置退出循环条件
        break
    SlowSnail(s)  # 调用函数

posted @ 2023-06-19 00:54  放氮气的蜗牛  阅读(389)  评论(0编辑  收藏  举报  来源