Python3 字符串
描述
count() 方法方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。。
语法
count() 方法的语法:
str.count(sub, start= 0,end=len(string))
参数
返回值
该方法返回子字符串在字符串中出现的次数。
实例
以下实例展示了count()方法的实例:
#!/usr/bin/python3 str="www.runoob.com" sub='o' print ("str.count('o') : ", str.count(sub)) sub='run' print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
以上实例输出结果为:
str.count('o') : 3 str.count('run', 0, 10) : 1