count()方法返回出现在范围内串子数range [start, end]。可选参数的start和end都解释为片符号。
语法
以下是count()方法的语法:
str.count(sub, start= 0,end=len(string))
参数
返回值
此方法返回集中在长度宽度的字符串。
例子
下面的例子显示了count()方法的使用。
#!/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, 4, 40) : 1