# 统计字符串中大写字母、小写字母、数字、其他字符的个数
def countCharacters(s):
# 统计大写字母的个数
upper = sum([1 for c in s if c.isupper()])
# 统计小写字母的个数
lower = sum([1 for c in s if c.islower()])
# 统计数字的个数
digit = sum([1 for c in s if c.isdigit()])
# 统计其他字符的个数
other = len(s) - upper - lower - digit
# 返回结果元组
return upper, lower, digit, other
# 从键盘输入字符串
s = input("请输入用于统计的字符串:")
# 调用函数,并输出函数返回的元组结果
print(countCharacters(s))
代码如下,望采纳:
import re
#大写字母、小写字母、数字、其他字符的个数
def counter(str1):
up_alpha = 0
low_alpha = 0
digit = 0
other = 0
for i in str1:
if re.findall(r"[A-Z]", i):
up_alpha += 1
elif re.findall(r"[a-z]", i):
low_alpha += 1
elif re.findall(r"\d", i):
digit += 1
else:
other += 1
return tuple([up_alpha,low_alpha,digit,other])
a = input('请输入字符串:')
res = counter(a)
print("统计结果:{}".format(res))
def count():
voc = input("请输入一串字符:")
alist = [0, 0, 0, 0]
# 四项数字依次为大写字母、小写字母、数字、其他字符的个数
for i in voc:
if 'A' <= i <= 'Z':
alist[0] += 1
elif 'a' <= i <= 'z':
alist[1] += 1
elif '0' <= i <= '9':
alist[2] += 1
else:
alist[3] += 1
info_tuple = tuple(alist)
print("大写字母、小写字母、数字、其他字符的个数分别为:")
print(info_tuple)
count()