Python String count() function returns the number of occurrences of a substring in the given string.

Python String count()函数返回给定字符串中子字符串出现的次数。

Python字符串count() (Python String count())

The count() function syntax is:

count()函数的语法为:

str.count(sub[, start[, end]])

sub is the substring to find in the given string.

sub是在给定字符串中找到的子字符串。

start is an optional argument. It specifies the starting index position from where the substring is looked for. Its default value is 0.

start是一个可选参数。 它指定从中查找子字符串的起始索引位置。 默认值为0。

end is an optional argument. It specifies the ending index position in the specified string. Its default value is the length of the string.

end是一个可选参数。 它指定指定字符串中的结束索引位置。 它的默认值是字符串的长度 。

Let’s look at a simple example of string count() function.

让我们看一个简单的字符串count()函数示例。

s = 'I like Python programming. Python is Awesome!'print(f'Number of occurrence of "Python" in String = {s.count("Python")}')print(f'Number of occurrence of "Python" in String between index 0 to 20 = {s.count("Python", 0, 20)}')

Output:

输出:

Number of occurrence of "Python" in String = 2
Number of occurrence of "Python" in String between index 0 to 20 = 1

Recommended Reading: Python f-strings

推荐读物: Python f字符串

Let’s look at another example where the user will enter the string and substring and we will print the count of occurrences using count() function.

让我们看另一个示例,在该示例中,用户将输入字符串和子字符串,并使用count()函数打印出现次数。

s = input('Please enter a string:\n')sub = input('Please enter a sub-string:\n')print(f'Number of occurrence of "{sub}" in the "{s}" is {s.count(sub)}')

Output:

输出:

Please enter a string:
a,e,i,o,u
Please enter a sub-string:
,
Number of occurrence of "," in the "a,e,i,o,u" is 4Please enter a string:
Hello World from Python Tutorials from JournalDev
Please enter a sub-string:
from
Number of occurrence of "from" in the "Hello World from Python Tutorials from JournalDev" is 2
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/23754/python-string-count

Python字符串count()相关推荐

  1. [转载] python 字符串查找的4个方法和count函数

    参考链接: Python字符串 count 方法 python 字符串查找的4个方法和count函数 所谓的字符串查找方法就是查找子串在字符串中的位置和出现的次数 python 字符串查找有4个方法, ...

  2. 真香!精心整理了 100+Python 字符串常用操作

    来源丨萝卜大杂烩 作者丨周萝卜 字符串作为平时使用最多的数据类型,其常用的操作我们还是很有必要熟记于心的,本文整理了多种字符串的操作的案例,还是非常用心,记得点赞收藏~ 字符串切片操作 test = ...

  3. python字符串出栈方法_python字符串常用方法

    1. isalnum() :判断字符串所有的字符都是字母或者数字.返回true和false In [1]: str1='jiangwei520' In [2]: str2='jiang wei' In ...

  4. Python字符串处理函数

    Python字符串处理函数简明 返回被去除指定字符的字符串 默认去除空白字符 删除首尾字符:str.strip([char]) 删除首字符:str.lstrip([char]) 删除尾字符str.st ...

  5. python字符串find函数-Python内置的字符串处理函数整理

    str='python String function' 生成字符串变量str='python String function' 字符串长度获取:len(str) 例:print '%s length ...

  6. Python字符串基本操作

    Python字符串基本操作 1.判断是不是合法的标识符isidentifier name="ABC" print(name.isidentifier()) 打印结果 True 2. ...

  7. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息. 在编程中,几 ...

  8. Python字符串必须知道的7个函数

    Python字符串函数数不胜数,想要记完所有几乎不可能,下列几个是极为重要的一些函数,属于必记函数. 一.join 功能: 将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 语 ...

  9. Python字符串类型及操作(含实例)

    字符串 由0个或多个字符组成的有序字符序列 字符串是字符的有序序列,可以对其中的字符进行索引 "智"是"智慧女孩千万不要秃头"的第0个字符 字符串有2类共四种表 ...

最新文章

  1. 图像有用区域 bfs
  2. sun服务器dt消息不能被启动,Sun ONE Application Server 错误
  3. VS2010下如何改变Button颜色、背景、边框
  4. Per-class allocator 1
  5. 快速排序中Partition算法总是从右边开始查找的原因
  6. 从APP到API:金融科技C端到B端的思考
  7. 微信公众号开发--自定义菜单跳转页面并获取用户信息
  8. MPC-BE 1.6.0.6370 Beta 经典多媒体播放器
  9. unity 弹痕_Unity C# 用图片融合做弹孔
  10. 阿里巴巴-1688-退款退货明细下载(导出)
  11. 利用ArcGIS软件将csv文件转换为shp格式
  12. 程序员多数性功能不行_不会盲打的程序员和不懂技术的 CTO
  13. uniapp开发中ios底部出现安全距离问题怎么解决?
  14. linux系统中pinctrl 和gpio子系统使用方法(教你点灯)
  15. python info函数的使用方法_sysinfo函数使用方法
  16. 兼容ie浏览器代码处理
  17. 浅析项目工作量估算方法
  18. pandas一行代码绘制26种美图
  19. java考了80多分,八省联考成绩出炉,学生们表示“有些崩溃”,你考了多少分
  20. 我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer

热门文章

  1. javaScript = == ===的区别
  2. IOS开发之获得当前的网速
  3. (zt)svn 随服务器启动
  4. [转载] [转载] python set集合如何有序输出_python set集合的用法
  5. LODOP打印当前日期时间的方法
  6. 为什么公司宁愿 25K 重新招人,也不给你加到 20K?原因太现实……
  7. Invalid format of Import utility nameVerify that ORACLE_HOME is properly oracle11.2g 无法imp,dmp
  8. centos上自动执行脚本执行php文件
  9. VxWorks 6.9 内核编程指导之读书笔记 -- POSIX
  10. 镜像配置见证机失败解决方案