售前咨询:400-100-2938
可以使用Python中的`count()`方法来计算字符串中子串出现的次数。
示例代码如下:
```python
s = "hello world hello"
sub = "hello"
count = s.count(sub)
print(count)
```
以上代码输出结果为2,即子串"hello"在字符串"s"中出现了2次。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Python中怎么计算字符串中特定字符出现的次数