Find all occurrences of a substring in Python

Posted by cru3l on Stack Overflow See other posts from Stack Overflow or by cru3l
Published on 2011-01-12T02:35:18Z Indexed on 2011/01/12 2:53 UTC
Read the original article Hit count: 134

Filed under:
|

Python has string.find() and string.rfind() to get the index of a substring in string.

I wonder, maybe there is something like string.find_all() which can return all founded indexes (not only first from beginning or first from end)?

For example:

string = "test test test test"

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

#that's the goal
print string.find_all('test') # [0,5,10,15]

© Stack Overflow or respective owner

Related posts about python

Related posts about string