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

Manprit Singh manpritsinghece at gmail.com
Fri Oct 30 11:23:21 EDT 2020


Dear sir ,

Consider a string
st1 = "I am a boy, i am an engineer, i am a genius"

i have to find the index of every occurrence word "am" in the string st1. i
have written a program below:

st1 = "I am a boy, i am an engineer, i am a genius"
w ="am"
for i in range(len(st1)):
    if st1.find(w, i, i + len(w)) != -1:
        print(i)
need to know, if this problem can be done in a more clean way ? Kindly give
some hints so that i can rewrite it in a more clean and clear way .

Regards
Manprit Singh


More information about the Tutor mailing list