Python/ref string count
来自菜鸟教程
<languages />
Python String count()方法
例
返回值“ apple”出现在字符串中的次数:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x)
定义和用法
The
count()
方法返回指定值在字符串中出现的次数。
句法
string.count(value, start, end)
参数值
| 参数 | 描述 |
|---|---|
| value | 需要。一个字符串。要搜索的值的字符串 |
| start | 可选的。整数。开始搜索的位置。默认为0 |
| end | 可选的。整数。结束搜索的位置。默认是字符串的结尾 |
更多例子
例
从位置10到24搜索:
txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple",
10, 24)
print(x)