Python 字符串统计:大写字母、小写字母和数字字符计数
s = input('请输入字符串:')
upper_count = 0 # 大写字母计数器
lower_count = 0 # 小写字母计数器
digit_count = 0 # 数字字符计数器
for c in s:
if c.isupper(): # 判断是否为大写字母
upper_count += 1
elif c.islower(): # 判断是否为小写字母
lower_count += 1
elif c.isdigit(): # 判断是否为数字字符
digit_count += 1
print('大写字母个数:', upper_count)
print('小写字母个数:', lower_count)
print('数字字符个数:', digit_count)
示例输出:
请输入字符串:Hello World! 123
大写字母个数: 2
小写字母个数: 8
数字字符个数: 3
原文地址: https://www.cveoy.top/t/topic/nVly 著作权归作者所有。请勿转载和采集!