Python 字符串统计:大写字母、小写字母和数字个数
你可以使用以下代码来实现这个功能:
def count_chars(string):
upper_count = 0
lower_count = 0
digit_count = 0
for char in string:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
return upper_count, lower_count, digit_count
string = input('请输入一个字符串:')
upper_count, lower_count, digit_count = count_chars(string)
print('大写字母个数:', upper_count)
print('小写字母个数:', lower_count)
print('数字个数:', digit_count)
在运行代码时,会要求你输入一个字符串。然后,程序会统计字符串中的大写字母、小写字母和数字的个数,并输出结果。
原文地址: https://www.cveoy.top/t/topic/mT5H 著作权归作者所有。请勿转载和采集!