我是靠谱客的博主 欣喜板凳,这篇文章主要介绍Python统计字符串中的中英文字符、数字空格,特殊字符,现在分享给大家,希望可以做个参考。

复制代码
1
# -*- coding:utf8 -*-
import string
from collections import namedtuple

def str_count(s):
'''找出字符串中的中英文、空格、数字、标点符号个数'''
count_en = count_dg = count_sp = count_zh = count_pu = 0

s_len = len(s)
for c in s:
# 英文
if c in string.ascii_letters:
count_en += 1
# 数字
elif c.isdigit():
count_dg += 1
# 空格
elif c.isspace():
count_sp += 1
# 中文
elif c.isalpha():
count_zh += 1
# 特殊字符
else:
count_pu += 1

total_chars = count_zh + count_en + count_sp + count_dg + count_pu
if total_chars == s_len:
return namedtuple('Count&

最后

以上就是欣喜板凳最近收集整理的关于Python统计字符串中的中英文字符、数字空格,特殊字符的全部内容,更多相关Python统计字符串中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部