相关疑难解决方法(0)

2149
推荐指数
17
解决办法
65万
查看次数

在Python中查找所有出现的子字符串

Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.

我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?

例如:

string = "test test test test"

print string.find('test') # 0
print string.rfind('test') # 15

#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud)

python regex string

325
推荐指数
12
解决办法
39万
查看次数