统计字符串中大小写字母个数

编写函数,接收字符串参数,返回一个元组,其中第一个元素为大写字母个数,第二个元素为小写字母个数。

import string
import random
#利用string模块函数生成字符串abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
x=string.ascii_letters+string.digits+string.punctuation
def fun(s):
    upper_count=0
    lower_count=0
    for i in s:
        if i.isupper():
            upper_count+=1
        elif i.islower():
            lower_count+=1
        else:
            continue
    return upper_count,lower_count
if __name__ == '__main__':
    #随机选取10个字符
    s=''.join([random.choice(x) for i in range(10)])
    print(s)
    print(fun(s))

发布了51 篇原创文章 · 获赞 5 · 访问量 2142

猜你喜欢

转载自blog.csdn.net/weixin_44659084/article/details/103325174