Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''Exercise to complete printLocations as described below.'''
- 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, everywhere!', 'ere')
- 1
- 8
- 20
- '''
- repetitions = s.count(target)
- end_position = 0
- start_position = s.find(target)
- for i in range(repetitions):
- start_position = s.find(target, end_position)
- end_position = s.find(target[-1], start_position) + 1
- print(start_position)
- def main():
- phrase = 'Here, there, everywhere!'
- print('Phrase:', phrase)
- for target in ['ere', 'er', 'e', 'eh', 'zx']:
- print('finding:', target)
- print(printLocations(phrase, target))
- print('All done!')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement