## Alan Lee

Create some code, change small world

# 代码

# coding: utf-8

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', ['total', 'zh', 'en', 'space', 'digit', 'punc'])(s_len, count_zh, count_en, count_sp, count_dg, count_pu)
else:
print('Something is wrong!')
return None
return None

s = '上面是引用了官网的介绍，意思就是说 TensorBoard 就是一个方便你理解、调试、优化 TensorFlow 程序的可视化工具，你可以可视化你的 TensorFlow graph、学习参数以及其他数据比如图像。'
count = str_count(s)
print(s, end='\n\n')
print('该字符串共有 {} 个字符，其中有 {} 个汉字，{} 个英文，{} 个空格，{} 个数字，{} 个标点符号。'.format(count.total, count.zh, count.en, count.space, count.digit, count.punc))

\$ python str_count.py

# END

#### python统计字母、空格、数字等字符个数

2017-04-06 15:29:24

#### python小练习1：给一个字符串，统计其中的数字、字母和其他类型字符的个数

2016-10-12 18:03:01

#### Python统计字符串里某个字符出现的次数

2009-08-05 12:42:00

#### python 统计字符串中的字符个数

2018-03-27 15:13:16

#### python之统计字符串中的字符个数

2018-04-23 13:28:00

#### 使用python统计字符串中的字符个数。

2016-10-10 19:32:07

#### 【python】统计文件中的字符串数目

2017-11-02 14:10:43

#### python笔记：案例之 统计字符串里各种字符的个数

2018-04-14 21:27:19

#### Python一日一练19----统计字符串中的字符个数

2016-04-29 15:44:02

#### Python---统计字符串中各字符的个数

2018-03-27 21:34:10