import re def calcultor(content): num = len(re.findall('[0-9]',content)) alpah = len(re.findall('[a-zA-Z]',content)) blank = len(re.findall(' ',content)) others = len(re.findall('[^0-9a-zA-Z ]',content)) print('数字:%s , 字母:%s ,空格:%s,其他:%s'%(num,alpah,blank,others)) calcultor('a s****-') #数字:0 , 字母:2 ,空格:2,其他:5
def calcultor(content): res = {'num':0,'alpha':0,'blank':0,'other':0} for i in content: if i.isdigit(): res['num'] += 1 elif i.isalpha(): res['alpha'] += 1 elif i.isspace(): res['blank'] += 1 else: res['other'] += 1 msg = '数字:%s,字母:%s,空格:%s,其他:%s'%(res['num'],res['alpha'],res['blank'],res['other']) print(msg) calcultor('**123') #数字:3,字母:0,空格:0,其他:2