Search substring in a string and get index of all occurances

Tim Chase python.list at tim.thechases.com
Wed Jun 21 08:38:57 EDT 2006


> I would like to search for a substring in a string and get the index of 
> all occurances.
> 
> mystring = 'John has a really nice powerbook.'
> substr = ' '  # space
> 
> I would like to get this list:
>    [4, 8, 10, 17, 22]
> 
> How can I do that without using "for i in mystring" which might be 
> expensive for large strings?

 >>> mystring = 'John has a really nice powerbook.'
 >>> substr = ' '
 >>> indicies = [i for i in xrange(len(mystring)) if 
mystring.startswith(substr, i)]
 >>> indicies
[4, 8, 10, 17, 22]

is my preferred way of doing this.  Theoretically, it doesn't 
involve copying any bits of the string, as startswith(substring, 
offset) *should* be smart enough to do the check internally 
without copying pieces of mystring for comparison, just to return 
whether a submatch starts there.  Whether it *does* do that is 
another matter for the higher python powers.  It also uses xrange 
which shouldn't create a temporary array of indicies, but rather 
use an iteratable sequence generator.

It should work for finding all 3 instances of "aba' in "abababa" 
as well, another common query of a similar form here on the list.

-tkc






More information about the Python-list mailing list