方法一,排除法
假设只有中英文字符:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import string
def str_count(str): '''找出字符串中的中英文、空格、数字、标点符号个数''' count_en = count_dg = count_sp = count_zh = count_pu = 0
for s in str: if s in string.ascii_letters: count_en += 1 elif s.isdigit(): count_dg += 1 elif s.isspace(): count_sp += 1 elif s.isalpha(): count_zh += 1 else: count_pu += 1
print('英文字符:', count_en) print('数字:', count_dg) print('空格:', count_sp) print('中文字符:', count_zh) print('特殊字符:', count_pu)
s = 'dfajl!大家@发!# 管道·符了3 54沙3发开fs\][dj' str_count(s)
|
方法二,范围判断
Unicode 中,基本中文字符处在一个范围区间,可以参考 汉字 Unicode 编码范围。写成代码就是:
1 2 3 4 5 6 7 8 9 10
| def hans_count(str): hans_total = 0 for s in str: if '\u4e00' <= s <= '\u9fef': hans_total += 1 return hans_total
s = 'dfajl!大家@发!# 管道·符了3 54沙3发开fs\][dj' print(hans_count(s))
|
参考资料:
- python统计中文字符数量
- 汉字 Unicode 编码范围