首页 > 技术文章 > python--字符串练习

python-cat 2020-01-02 10:17 原文

输⼊⼀个字符串,要求判断在这个字符串中⼤写字⺟,⼩写字⺟,数字, 其它字符共出现了多少次,并输出

-*- coding:utf-8 -*-
content = input('请输入一个字符串:')
index = 0
content_len = len(content)
num_count = 0
upper_str_count = 0
lower_str_count = 0
other_count = 0
while True:
    if index >= content_len:
        break
    else:
        val = content[index]
        if val.isdigit():
            num_count += 1
        elif val.isupper():
            upper_str_count += 1
        elif val.islower():
            lower_str_count +=1
        else:
            other_count += 1
    index += 1
message = '''
==============输入字符串信息:%s=================
            大写字母出现了 %d次
            小写字母出现了 %d次
                数字出现了 %d次
            其它字符出现了 %d次
==============================================
'''
print(message %(content,upper_str_count,lower_str_count,num_count,other_count))

 

推荐阅读