1
2def find_all_indexes(input_str, search_str):
3 l1 = []
4 length = len(input_str)
5 index = 0
6 while index < length:
7 i = input_str.find(search_str, index)
8 if i == -1:
9 return l1
10 l1.append(i)
11 index = i + 1
12 return l1
13
14
15s = 'abaacdaa12aa2'
16print(find_all_indexes(s, 'a'))
17print(find_all_indexes(s, 'aa'))
18
1The find() method returns the index of first occurrence of the substring
2(if found). If not found, it returns -1.
3message = 'Python is a fun programming language'
4
5# check the index of 'fun'
6print(message.find('fun'))
7
8# Output: 12