python count函数用法详解-Python教程

资源魔 48 0

正在python中能够应用“count()”函数统计字符串里某个字符呈现的次数,该函数用于统计次数,其语法是“count(sub, start= 0,end=len(string))”,其参数sub示意搜寻的子字符串。

Python count() 办法用于统计字符串里某个字符呈现的次数。可选参数为正在字符串搜寻的开端与完结地位。

语法

count()办法语法:

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

  • 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) :  2
str.count(sub) :  1

保举教程: 《Python教程》

以上就是python count函数用法详解的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题

抱歉,评论功能暂时关闭!