Search substring in a string and get index of all occurances

Nico Grubert nicogrubert at
Wed Jun 21 10:28:58 CEST 2006

Hi there,

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?

Thanks in advance,

More information about the Python-list mailing list