python

python怎么计算次数

摘星
880
2021-05-28 16:42:23
栏目: 编程语言

python中计算次数的方法,在python中可以使用count()方法计算次数,该函数主要用于统计字符串里某个字符出现的次数,可选参数为在字符串搜索的开始与结束位置。

python怎么计算次数

具体内容如下:

count()语法

str.count(sub, start= 0,end=len(string))

参数说明:

sub:搜索的子字符串

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

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

示例:

#!/usr/bin/python

#www.yisu.com

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

0
看了该问题的人还看了