【Python练习1】统计一串字符中英文字母、空格、数字和其他字符的个数
练习思路:
1.输入一串字符
2.筛选出字符中的英文字母并统计
3.筛选出字符中的空格并统计
4.筛选出字符中的数字并统计
5.筛选出字符中的其他字符并统计
代码实现:
def msg(s): abc_num = 0 space_num = 0 digit_num = 0 other_num = 0 for i in s: if i.isalpha(): abc_num += 1 elif i.isspace(): space_num += 1 elif i.isdigit(): digit_num += 1 else: other_num += 1 return(abc_num, space_num, digit_num, other_num) m = msg('My name is Gavin & Tel = 18912360154.') print(m)
结果展示: