There is no simple built-in string function that does what you're looking for, but you could use the more powerful regular expressions:

import re
[m.start() for m in re.finditer('test', 'test test test test')]
#[0, 5, 10, 15]

If you want to find overlapping matches, lookahead will do that:

[m.start() for m in re.finditer('(?=tt)', 'ttt')]
#[0, 1]

If you want a reverse find-all without overlaps, you can combine positive and negative lookahead into an expression like this:

search = 'tt'
[m.start() for m in re.finditer('(?=%s)(?!.{1,%d}%s)' % (search, len(search)-1, search), 'ttt')]
#[1]

re.finditer returns a generator, so you could change the [] in the above to () to get a generator instead of a list which will be more efficient if you're only iterating through the results once.

Answer from moinudin on Stack Overflow
Discussions

Python Regex: Extract all occurences of a substring within a string - Stack Overflow
I am trying to extract all occurrences of a substring within a string using Python Regex. This is what I have tried: import re line = "The dimensions of the first rectangle: 10'x20', second More on stackoverflow.com
🌐 stackoverflow.com
How can I find all exact occurrences of a string, or close matches of it, in a longer string in Python?
This is a regex operation. You can match exact with some_substring in full_string Then find the index. But partial matches is going to require a lot of code or a regex expression. More on reddit.com
🌐 r/learnpython
8
1
May 9, 2024
Python regex when string contains any word from a list of words AND any word from another list

Use lookaheads. ^(?=.*foo|.*bar|.*Python)(?=.*me|.*you|.*we)

Add \b around the words (e.g. \bfoo\b) if you want them as isolated words, otherwise you get matches like fool)

https://regex101.com/r/dnqSjr/1

More on reddit.com
🌐 r/regex
5
3
December 8, 2022
how to extract only names in string by using regex
It seems like the pattern your searching for is "title, space, word". You can use 'Ms. ', 'Mrs. ', and 'Mr. ' for the title and space, but there are a ton of titles in the real world (stuff like 'Dr. ', 'Prof. ', 'Captain ', 'Reverend ', etc.) So one such regex string could be: r'(Ms. |Mrs. |Mr. )([A-Za-z]+)' More on reddit.com
🌐 r/learnpython
6
0
March 22, 2023
🌐
Delft Stack
delftstack.com › home › howto › python › python find all occurrences in string
How to Find All Substring Occurrences in Python String | Delft Stack
February 2, 2024 - In Python, to use the str.find() method to find all occurrences of a substring in a string, you can create a loop that iterates through the string and uses str.find() to locate the substring.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-all-occurrences-of-substring-in-string
Python - All occurrences of substring in string - GeeksforGeeks
July 12, 2025 - Track positions: Each found position is added to the positions list, and the start index is updated to move past the current match to continue searching for subsequent occurrences. List comprehension with range() can be used to generate all starting positions of a substring by iterating over the string indices.
🌐
TutorialsPoint
tutorialspoint.com › finding-all-occurrences-of-a-substring-in-a-python-string
Finding All Occurrences of a substring in a Python string
August 25, 2023 - The Regular expression pattern is created to match all the substring lists to the input string. Then by using findall() function from the re python library to find all occurrences of the string.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | list all occurrences of pattern in string
Python | List All Occurrences of Pattern in String - Be on the Right Side of Change
May 30, 2022 - To find all substrings in a given string, use the re.findall(substring, string) function that returns a list of matching substrings—one per match. import re s = 'Finxters learn Python with Finxter' pattern = 'Finxter' # Method 4: re.findall() ...
🌐
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 - This is due to the reason that the find() method returns the start index of a substring if it is found in the string. Then, we will move to the next execution of the for loop. If the find() method returns -1, we will move to the next execution of the for loop.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › python-count-number-of-substring-occurrences-in-string
Python: Count Number of Substring Occurrences in String
March 28, 2023 - Original string is: John has 1 apple, Sarah has 2 apples, Mike has 5 apples. Substring is: apples Number of substring occurrences is: 2 Starting indices of substrings are: [30, 49] The finditer() function is part of Python's RegEx library - re.
🌐
JanBask Training
janbasktraining.com › community › devops › how-to-find-all-occurrences-of-a-substring
How to find all occurrences of a substring? | JanBask Training Community
July 18, 2021 - Python has string.find() and string.rfind() to get the index of a substring in a string. I'm wondering whether there is something like string.find_all() which can return all found indexes (not only the first from the beginning or the first from the end). ... string = "test test test test" print string.find('test') # 0 print string.rfind('test') # 15 #this is the goal print string.find_all('test') # [0,5,10,15] ... To find all occurrences of a substring in a string Python does not have any built-in string function that does what you're looking for, but you could use the more powerful regular expressions:
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to find all occurrences of a substring in a list of strings with python
5 Best Ways to Find All Occurrences of a Substring in a List of Strings with Python - Be on the Right Side of Change
March 5, 2024 - strings = ['apple pie', 'banana pie', 'apple tart'] substring = 'apple' occurrences = [(index, string.find(substring)) for index, string in enumerate(strings) if substring in string] print(occurrences) ... By employing a list comprehension, this snippet not only checks for the presence of the substring using the in operator but also calls the find() method on the string to obtain the exact starting position of the substring. Regular expressions provide a powerful way to search for patterns. By using Python’s re module, you can find complex patterns within strings.
🌐
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
Python string objects have a built-in method called str.find(substring, starting_index=0), which comes in handy here. It returns the lowest index of the substring in str that is greater than or equal to starting_index if found. Otherwise, it returns -1. for original, substring in zip(orig_strs, ...
🌐
Quora
quora.com › How-do-you-find-the-occurrences-of-a-substring-in-a-string-in-Python
How to find the occurrences of a substring in a string in Python - Quora
Answer: Suppose you have a string >>> str = ‘now is the time for all good men to come to the aid of their country’ then >>> str.find('all') 20 ‘all’ starts at str[20] To find all the ‘to’ substrings, you need ‘regular expressions’. That’s too complicated to go into here.
🌐
CodeRivers
coderivers.org › blog › python-find-all-occurrences-in-string
Python: Finding All Occurrences in a String - CodeRivers
February 22, 2026 - This code works in a similar way ... re module in Python allows us to use regular expressions. To find all occurrences of a substring, we can use the finditer() function....
🌐
DataCamp
datacamp.com › tutorial › python-string-contains
Python String Search and Replace: in, .find(), .index(), .count(), and .replace() | DataCamp
March 31, 2026 - Use .index() when the substring must exist and an exception is the right failure mode. product = "USB-C Cable" print(product.index("Cable")) # 6 try: product.index("HDMI") except ValueError: print("Required label missing") .count() returns the number of non-overlapping occurrences.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 475566 › how-to-find-all-occurrences-of-a-substring-in-a-string-python
how to find all occurrences of a substring in a string python
March 19, 2014 - This complements @Gribouillis’s regex fix while providing a simple non-regex solution that directly answers @sruthiashok’s request. Use re.findall() — Gribouillis 1,391 Jump to Post ... We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge. Sign Up — It's Free! ... Libcurl C++ CURLE_FAILED_INIT. SSH Error -43. 6 · Extracting Faces from Videos Using Python Deepface Library 0
🌐
LabEx
labex.io › tutorials › python-how-to-use-re-findall-in-python-to-find-all-matching-substrings-415132
How to use re.findall() in Python to find all matching substrings | LabEx
## Using re.findall() to find all occurrences of "Python" matches = re.findall(r"Python", text) ## Print the results print("Original text:") print(text) print("\nMatches found:", len(matches)) print("Matching substrings:", matches) ... Original text: Python is amazing. Python is versatile. I love learning Python programming. Matches found: 3 Matching substrings: ['Python', 'Python', 'Python'] ... The r before the string denotes a raw string, which is recommended when working with regular expressions
🌐
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!
It gives you the index of the found string. You can tell it where to start looking. Then write a little loop, find all the indexes and put them in a list. Nice little practice task. Since the RegEx solution is already given, and it's nicely short anyway, let me add the more lengthy builtin style version for reference: occurrences = [] word = 'abracadabra' i = 0 while True: f = word.find('abr', i) if f==-1: break occurrences.append(f) i = f+1 print(occurrences)
🌐
Finxter
blog.finxter.com › 5-best-ways-to-find-all-occurrences-of-a-substring-within-a-list-of-strings-in-python
5 Best Ways to Find All Occurrences of a Substring within a List of Strings in Python – Be on the Right Side of Change
The outer list comprehension aggregates these lists into a list of lists, making it clear where each occurrence is found within the input list. Regular expressions are powerful for string searching. Python’s re.finditer() method returns an iterator yielding match objects over all non-overlapping ...
🌐
Reddit
reddit.com › r/learnpython › how can i find all exact occurrences of a string, or close matches of it, in a longer string in python?
r/learnpython on Reddit: How can I find all exact occurrences of a string, or close matches of it, in a longer string in Python?
May 9, 2024 -

Goal:

  • I'd like to find all exact occurrences of a string, or close matches of it, in a longer string in Python.

  • I'd also like to know the location of these occurrences in the longer string.

  • To define what a close match is, I'd like to set a threshold, e.g. number of edits if using the edit distance as the metric.

  • I'd also like the code to give a matching score (the one that is likely used to determine if a candidate substring is over the matching threshold I set).

How can I do so in Python?


Example:

long_string = """1. Bob likes classical music very much.
2. This is classic music!
3. This is a classic musical. It has a lot of classical musics.
"""

query_string = "classical music"

I'd like the Python code to find "classical music" and possibly "classic music", "classic musical" and "classical musics" depending on the string matching threshold I set.


Research: I found Checking fuzzy/approximate substring existing in a longer string, in Python? but the question focuses on the best match only (i.e., not all occurrences) and answers either also focuses on the best match or don't work on multi-word query strings (since the question only had a single-word query strings, or return some incorrect score (doesn't get a perfect score even for an exact match).