Python - All occurrences of substring in string Last Updated : 10 Jan, 2025 Comments Improve Suggest changes Like Article Like Report A substring is a contiguous occurrence of characters within a string. Identifying all instances of a substring is important for verifying various tasks. In this article, we will check all occurrences of a substring in String.Using re.finditer()re.finditer() returns an iterator yielding match objects for all non-overlapping matches of a pattern in a string that allows to check for specific patterns, such as digits, throughout the string. Python import re # Define the input string and substring s = "hello world, hello universe" substring = "hello" # Find all occurrences using re.finditer positions = [match.start() for match in re.finditer(substring, s)] print(positions) Output[0, 13] Explanation:Use re.finditer() to find matches: The re.finditer() function searches for all occurrences of the substring "hello" in the string "hello world, hello universe", returning an iterator of match objects.Extract start positions: A list comprehension is used to extract the starting position of each match using match.start().Using str.find() in a loopUsing str.find() in a loop allows to find all occurrences of a substring by repeatedly searching for the next match starting from the last found index. The loop continues until no more matches are found (when find() returns -1). Python s = "hello world, hello universe" substring = "hello" # Find all occurrences using str.find in a loop positions = [] start = 0 while True: start = s.find(substring, start) if start == -1: break positions.append(start) start += len(substring) print(positions) Output[0, 13] ExplanationUse 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".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.Using List Comprehension with range():List comprehension with range() can be used to generate all starting positions of a substring by iterating over the string indices. It checks each possible position within the string to see if the substring matches starting from that index. Python s = "hello world, hello universe" substring = "hello" # Find all occurrences using list comprehension positions = [i for i in range(len(s)) if s.startswith(substring, i)] print(positions) Output[0, 13] ExplanationUse list comprehension with startswith(): The list comprehension iterates over each index i in the string, checking if the substring "hello" starts at that position using text.startswith(substring, i).Store starting positions: If the substring matches at index i, that index is added to the positions list. Comment More infoAdvertise with us Next Article Python - All occurrences of substring in string M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Get Second Occurrence of Substring in Python String We are given a string and a substring, and our task is to find the index of the second occurrence of that substring within the string. This means we need to identify not just if the substring exists, but where it appears for the second time. For example, if we have a string like "hello world, hello 2 min read Python - All occurrences of Substring from the list of strings Given a list of strings and a list of substring. The task is to extract all the occurrences of a substring from the list of strings. Examples: Input : test_list = ["gfg is best", "gfg is good for CS", "gfg is recommended for CS"] subs_list = ["gfg", "CS"] Output : ['gfg is good for CS', 'gfg is reco 5 min read Python | Get the string after occurrence of given substring The problem involves getting the string that is occurring after the substring has been found. Let's discuss certain ways in which this task can be performed using Python.Using partition()To extract the portion of a string that occurs after a specific substring partition() method is an efficient and 3 min read Python | Ways to find nth occurrence of substring in a string Given a string and a substring, write a Python program to find the nth occurrence of the string. Let's discuss a few methods to solve the given task. Get Nth occurrence of a substring in a String using regex Here, we find the index of the 'ab' character in the 4th position using the regex re.findit 4 min read Python | Get the starting index for all occurrences of given substring Given a string and a substring, the task is to find out the starting index for all the occurrences of a given substring in a string. Let's discuss a few methods to solve the given task. Method #1: Using Naive Method Python3 # Python3 code to demonstrate # to find all occurrences of substring in # a 3 min read Like