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

yipeiwu_com7年前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脚本

#!/usr/bin/python # This was written for educational purpose only. Use it at your own risk...

Python判断对象是否相等及eq函数的讲解

摘要 什么是python对象的标识 python对象相等的判断 自定义python对象相等的条件 python对象的标识 python对象标识就是python对象自身的要...

django mysql数据库及图片上传接口详解

django mysql数据库及图片上传接口详解

前言 我们在 django-rest-framework解析请求参数 文章中完成了接口文档到参数解析, 一个完整的流程中还缺少对数据库的操作. 本篇内容为djang...

跟老齐学Python之大话题小函数(2)

跟老齐学Python之大话题小函数(2)

上一讲和本讲的标题是“大话题小函数”,所谓大话题,就是这些函数如果溯源,都会找到听起来更高大上的东西。这种思维方式绝对我坚定地继承了中华民族的优良传统的。自从天朝的臣民看到英国人开始踢足...

python中(str,list,tuple)基础知识汇总

python是一门动态解释型的强类型定义语言(先编译后解释) 动态类型语言 动态类型的语言编程时,永远也不用给任何变量指定数据类型,该语言会在你第一次赋值给变量时,在内部将数据类型记录下...