首页 > 文章列表 > python怎么统计中文字符

python怎么统计中文字符

Python
376 2022-08-07

python统计中文字符的方法:首先定义函数;然后将初始计数值设置为0;再输入“s.isalpha()”判断是否为中文字符,如果是则计数值加1;最后打印出计数值结果即可。

def str_count(str):
    '''找出字符串中的中文字符的个数'''
        count_zh =  0
    for s in str:
         # 中文
        if s.isalpha():
            count_zh += 1
    print('中文:', count_zh)