def printLocations(s, target):
'''
s is a string to search through, and target is the substring to look for.
Print each index where the target starts.
For example:
>>> printLocations('Here, there, everywherel', 'ere')
1
8
20
'''
repetitions = s.count(target)
index = -1
for i in range(repetitions):
index = s.find(target, index+1)
print(index)
def main():
phrase="Here, there, everywhere!"
print('Phrasez', phrase)
for target in ['ere', 'er', 'e', 'eh', 'zx']:
print('finding:', target)
printLocations(phrase, target)
print('All done!')
main()
Demo:
Phrasez Here, there, everywhere!
finding: ere
1
8
20
finding: er
1
8
15
20
finding: e
1
3
8
10
13
15
20
22
finding: eh
finding: zx
All done!
3
solved Print Location of a string [closed]