统计字符:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
import re
s = input('请输入一串字符:')
char=re.findall(r'[a-zA-Z]',s)#以列表类型返回全部能匹配的子串
num=re.findall(r'[0-9]',s)
blank=re.findall(r' ',s)
chi=re.findall(r'[\u4E00-\u9FFF]',s)#汉字的Unicode编码范围
other = len(s)-len(char)-len(num)-len(blank)-len(chi)
print('字母',len(char),'\n数字',len(num),'\n空格',len(blank),'\n中文',len(chi),'\n其他',other)
s = input('统计字符串:')
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print(f'英文字母 {letters}个、空格 {space}个、数字 {digit} 个和其他字符 {others} 个')