编写函数count,统计给定字符串中大写字母、小写字母、数字字符及其他字符的出现次数。
2条回答 默认 最新
- chuifengde 2021-12-27 09:53关注
import string def count(s): d = {} for i in s: if i in string.ascii_uppercase: d['big'] = d.get('big', 0) + 1 elif i in string.ascii_lowercase: d['small'] = d.get('small', 0) + 1 elif i in string.digits: d['digit'] = d.get('digit', 0) + 1 else: d['other'] = d.get('other', 0) + 1 return d res = count('asUO223aswoe w2347(*&*(') print(res) '''--result {'small': 8, 'big': 2, 'digit': 7, 'other': 7} '''
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用