Python,编写一个函数

  1. 编写函数
    接收一个字符串,分别统计大写字母、小写字母、数字、其他字符的个数,并返回结果。

# coding: UTF-8
 
 
def count_test(st):
    cap, small, num, other = 0, 0, 0, 0
    for i in st:
        if i.isupper():
            cap = cap + 1
        elif i.islower():
            small = small + 1
        elif i.isdigit():
            num = num + 1
        else:
            other = other + 1
    print('大写字母个数:%dn小写字母个数:%dn数字个数:%dn其他字符个数:%dn' % (cap, small, num, other))
 
 
string = input('输入字符串:')
count_test(string)

如果有帮助,点一下下采纳


import re


def count(string):
    upper = re.findall('[A-Z]{1}', string)
    print(f'大写数量:{len(upper)}')
    lower = re.findall('[a-z]{1}', string)
    print(f'小写数量:{len(lower)}')
    number = re.findall('[0-9]{1}', string)
    print(f'数字数量:{len(number)}')
    print(f'其他字符数量:{len(string) - len(upper) - len(lower) - len(number)}')


if __name__ == '__main__':
    count('ADSFkssf123   .sdfjUJKnaljJ')