可以使用Python中的count()方法来计算字符串中子串出现的次数。
count()
示例代码如下:
s = "hello world hello" sub = "hello" count = s.count(sub) print(count)
以上代码输出结果为2,即子串”hello”在字符串”s”中出现了2次。