Using regular expressions, you can use re.finditer to find all (non-overlapping) occurences:

>>> import re
>>> text = 'Allowed Hello Hollow'
>>> for m in re.finditer('ll', text):
         print('ll found', m.start(), m.end())

ll found 1 3
ll found 10 12
ll found 16 18

Alternatively, if you don't want the overhead of regular expressions, you can also repeatedly use str.find to get the next index:

>>> text = 'Allowed Hello Hollow'
>>> index = 0
>>> while index < len(text):
        index = text.find('ll', index)
        if index == -1:
            break
        print('ll found at', index)
        index += 2 # +2 because len('ll') == 2

ll found at  1
ll found at  10
ll found at  16

This also works for lists and other sequences.

Answer from poke on Stack Overflow
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
Below is a summary of what you need to remember about each Python string method. str.find(), str,index(): These return the lowest index of the substring.
Discussions

How can I find all indices where a substring appears in a string?
Question In the context of this exercise, how can I find all indices where a substring appears in a string? Answer In this exercise, we were introduced to the .find() method, but it will only return the first index where the substring appears in a string. To obtain all the indices where a substring ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
11
October 30, 2018
Help with .index()? finding multiple instances of item
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself. More on reddit.com
🌐 r/learnpython
9
3
March 11, 2023
python - Function to find all occurrences of substring - Code Review Stack Exchange
This function returns a list of all the beginning indices of a substring in a string. After finding the index of a substring, the search for the next one begins just after this index. def find_sub... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
December 11, 2016
Find all substrings
word = input("Input Word") target_char = input("Input Character") SUBSTRING_LEN = 3 for idx, char in enumerate(word[: -(SUBSTRING_LEN - 1)]): if char == target_char: print(word[idx : idx + SUBSTRING_LEN]) More on reddit.com
🌐 r/learnpython
10
2
December 20, 2023
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How can I find all indices where a substring appears in a string? - Python FAQ - Codecademy Forums
October 30, 2018 - Answer In this exercise, we were introduced to the .find() method, but it will only return the first index where the substring appears in a string. To obtain all the indices where a substring appears, you can use a loop to iterate over the entire ...
🌐
datagy
datagy.io › home › python posts › python strings › python: find an index (or all) of a substring in a string
Python: Find an Index (or all) of a Substring in a String • datagy
December 20, 2022 - Learn how to find the index of the first or last substring of a string using Python. Also learn how to find all indices of a substring.
🌐
GeeksforGeeks
geeksforgeeks.org › python-all-occurrences-of-substring-in-string
Python - All occurrences of substring in string - GeeksforGeeks
January 10, 2025 - Use find() in a loop: The find() method is called repeatedly, starting from the last found position, to locate each occurrence of the substring "hello" in the string "hello world, hello universe".
🌐
CodeSignal
codesignal.com › learn › courses › practicing-string-operations-and-type-conversions-in-python › lessons › exploring-substring-search-in-python-strings
Exploring Substring Search in Python Strings
We use the built-in zip() function to create pairs of original strings and substrings. We then use the find() method to find the first occurrence of each substring in its related original string. Python string objects have a built-in method called str.find(substring, starting_index=0), which ...
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › python-find-indices-of-all-occurrences-of-character-in-string
Find all occurrences of a Substring in a String in Python | bobbyhadz
We used a while loop to iterate for as long as the start variable is less than the string's length. On each iteration, we use the str.find() method to find the next index of the substring in the string.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Search for a String in Python (Check If a Substring Is Included/Get a Substring Position) | note.nkmk.me
May 7, 2023 - The rfind() method searches the string starting from the right side. Built-in Types - str.rfind() — Python 3.11.3 documentation · If multiple substrings are present, the position of the rightmost substring is returned.
🌐
Reddit
reddit.com › r/learnpython › help with .index()? finding multiple instances of item
r/learnpython on Reddit: Help with .index()? finding multiple instances of item
March 11, 2023 -

Taken from https://www.programiz.com/python-programming/online-compiler/?ref=409055e9 :

vowels = ['a', 'e', 'i', 'o', 'i', 'u']

# index of the first 'i' is returned

index = vowels.index('i')

print('The index of i:', index)

Output: The index of i: 2

Say that the list was much bigger, and you don't know the contents, but you know 'i' is in it more than once. What would be the best way to find all instances of 'i'?

Thanks so much! <3

Top answer
1 of 5
5
The easy way would be to just use enumerate on the list, and then manually iterate the list and find the indices yourself.
2 of 5
3
str.index() has parameters start and end, which can be used to define the starting index for the search. So, if you know that you have more than one substring in your string, you can do the following: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = test.index('i') // i_idx -> 12 i_idx = test.index('i', i_idx+1) // i_idx -> 16 You can use it in the loop, but note that the str.index() throws an exception if the substring is not found. So, you can loop until you stop finding the substring in your string like this: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while True: try: i_idx = test.index('i', i_idx+1) print(i_idx) except ValueError as e: print(e) break Output: 12 16 42 45 61 65 substring not found There is a sibling function, str.find(). It works the same, but it does not raise exception, instead it returns -1 when the substring is not found, so you can loop until the result of str.find() is -1 to find indices of all substring occurrences in your string: test = 'Lorem Ipsum is simply dummy text of the printing and typesetting industry' i_idx = 0 while (i_idx := test.find('i', i_idx+1)) > 0: print(i_idx) Output: 12 16 42 45 61 65 There is a function str.count() which can be used in a loop as well: i_idx = 0 for _ in range(test.count('i')): i_idx = test.index('i', i_idx+1) print(i_idx) The disadvantages of all the above method is that you have a time complexity of O(m*n) for a string with length n and m occurences of your substring in your string, with or O(n**2) for the worst case. Better time complexity can be achieved if you simply iterate through the string only once: for idx, char in enumerate(test): if char == 'i': print(idx) or using comprehensions generator expression (as pointed out by u/kyber/ : indices = (idx for idx, char in enumerate(test) if char == 'i') print(*indices)
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-the-index-of-a-substring-in-python
Find the Index of a Substring in Python - GeeksforGeeks
July 23, 2025 - s = "Geeks for Geeks" # Define the substring we want to find s1 = "for" parts = s.split(s1) # Check if the substring is in the string if len(parts) > 1: # Calculate the starting index of the substring index = len(parts[0]) print(f"The substring '{s1}' is found at index {index}.") else: print(f"The substring '{s1}' is not present in the text.") ... The substring 'for' is found at index 6. The re.search() in Python's re module can be used to locate the first occurrence of a pattern in a string.
🌐
Medium
medium.com › better-programming › 5-ways-to-find-the-index-of-a-substring-in-python-13d5293fc76d
5 Ways to Find the Index of a Substring in Python | by Indhumathy Chelliah | Better Programming
September 2, 2021 - str.find() returns the lowest index in the string where the substring sub is found within the slice s[start:end]. It returns -1 if the sub is not found. start and end are optional arguments. -python docs
🌐
PythonForBeginners.com
pythonforbeginners.com › home › find all occurrences of a substring in a string in python
Find All Occurrences of a Substring in a String in Python - PythonForBeginners.com
July 8, 2022 - To find all the occurrences of a substring in a string in python using a for loop, we will use the following steps. First, we will find the length of the input string and store it in the variable str_len. Next, we will find the length of the substring and store it in the variable sub_len.
🌐
Sololearn
sololearn.com › en › Discuss › 2144485 › is-it-possible-to-find-the-index-of-all-occurrences-of-a-substring-in-a-a-string-in-python
Is it possible to find the index of all occurrences of a substring in a a string in python? | Sololearn: Learn to code for FREE!
For example, in the string “Coat ...n.com/cFb42mADJjO4/?ref=app · 24th Jan 2020, 7:00 AM · Fermi · + 2 · You can use the string method 'find'. It gives you the index of the found string....
🌐
Vultr Docs
docs.vultr.com › python › standard-library › str › index
Python str index() - Find Substring Index | Vultr Docs
November 26, 2024 - Set these parameters to define the desired slice of the string for the search. ... full_string = "Hello, welcome to Python. Welcome again!" substring = "welcome" limited_search = full_string.index(substring, 10) # Start searching from index 10 print(limited_search) Explain Code
🌐
Reddit
reddit.com › r/learnpython › find all substrings
r/learnpython on Reddit: Find all substrings
December 20, 2023 -

I'm doing MOOC Python 2023 and I need to return all substrings of a string. An example of what the code should do is this:

Please type in a word: mammoth

Please type in a character: m

mam

mmo

mot

My code is this:

word = input("Please type in a word: ")
char = input("Please type in a character: ")
iterate = 1
while True:
    index = word.find(char)
    third = index + 3
    string = word[index:third]
    if len(string) != 3:
        break
    else:
        print(string)
        word = word[iterate:]
        iterate += 1

It works for the word "mammoth", but not for other words. Like when I put in the word "incomprehensibility" and search for character "i", I get this output:

inc

ibi

ibi

ibi

ibi

ity

Any help is appreciated

🌐
Python Tutorial
pythontutorial.net › home › python string methods › python string index()
Python String index(): Locating the Index of a Substring in a String
December 28, 2020 - Since the string has two substrings 'Python', the index() method returns the lowest index where the substring found in the string. The following example uses the index() method to find the substring 'Python' in the string 'Python will, Python will rock you.' within the slice str[1:]: