count()方法

count(str,beg=0,end=len(string))

描述

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

语法

count()方法语法:

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

参数

  • sub -- 搜索的子字符串

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

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

返回值

该方法返回子字符串在字符串中出现的次数。

实例

以下实例展示了count()方法的实例:

str1 = "www.runoob.com"
sub = "o"
print("str.count('o')", str1.count(sub))

sub = "run"
print("str.conut('run',0,10)", str1.count(sub, 0, 10))

# 结果为
# str.count('o') 3
# str.conut('run',0,10) 1
原文地址:https://www.cnblogs.com/xiaohei001/p/10121095.html