[Tutor] program to find index of every occurrence of a particular word in a string

Mark Lawrence breamoreboy at gmail.com
Fri Oct 30 14:40:52 EDT 2020


On 30/10/2020 16:55, Manprit Singh wrote:

Do you ever take a blind bit of notice of what gets said on this list? 
You've been asked repeatedly *NOT* to top post yet you still do it.

Worse yet your code still doesn't work correctly as it still finds "am" 
in "spam".

> Dear sir ,
> Have tried one more and found it quite good :
> 
> st1 = "I am a boy, i am an engineer, i am a genius"
> w ="am"
> 
> ge = (i for i in range(st1.rfind(w) + 1) if st1.startswith(w, i))
> print(*ge)
> 
> Regards
> Manprit singh
> 

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list