python统计中文字符数量的两种方法

yipeiwu_com6年前Python基础

方法一:

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)

方法二:

def str_count2(str):
 for s in str:
  # 中文字符范围
  if '\u4e00' <= s <= '\u9fff':
   print(s, end="\t")

以上这篇python统计中文字符数量的两种方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

用Python制作检测Linux运行信息的工具的教程

在这篇文章里,我们将会探索如何使用Python语言作为一个工具来检测Linux系统各种运行信息。让我们一起来学习吧。 哪种Python? 当我提到Python时,我一般是指CPython...

查看python安装路径及pip安装的包列表及路径

查看python安装路径及pip安装的包列表及路径

一、Linux系统 查看Python路径 whereis python 此命令将会列出系统所安装的所有版本的Python的路径效果如下:  使用以下命令可分别查看Pytho...

对django layer弹窗组件的使用详解

父层: <div class="col-xs-12"> <div class="box"> <div class="box-header...

Python 基础知识之字符串处理

Python字符串处理 字符串输入: my_string = raw_input("please input a word:") 字符串判断: (1) 判断是不是纯字母 my_...

python读取并定位excel数据坐标系详解

python读取并定位excel数据坐标系详解

测试数据:坐标数据:testExcelData.xlsx 使用python读取excel文件需要安装xlrd库: xlrd下载后的压缩文件:xlrd-1.2.0.tar.gz 解压后再...