一推网

当前位置: 首页 > 知识问答 > python count函数用法详解

知识问答

python count函数用法详解

2026-11-22 19:34:25 来源:互联网转载

Python count() 方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

语法

count()方法语法:

str.count(sub, start= 0,end=len(string))
  • sub -- 搜索的子字符串

    立即学习“Python免费学习笔记(深入)”;

  • start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。

  • end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。

#!/usr/bin/python str = "this is string example....wow!!!"; sub = "i";print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)sub = "wow";print "str.count(sub) : ", str.count(sub)
str.count(sub, 4, 40) :  2str.count(sub) :  1

推荐教程: 《python-tutorials.html" target="_blank">Python教程》

python count函数

上一篇:36种漂亮的CSS3网页按钮Button样式,总有你喜欢的

下一篇:完美解决php无法上传大文件方法