Python | All occurrences of substring in string
Many times while working with strings, we have problems dealing with substrings. This may include the problem of finding all positions of a particular substrings in a string. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + startswith()
This task can be performed using the two functionalities. The startswith function primarily performs the task of getting the starting indices of substring and list comprehension is used to iterate through the whole target string.
# Python3 code to demonstrate working of # All occurrences of substring in string # Using list comprehension + startswith() # initializing string test_str = "GeeksforGeeks is best for Geeks" # initializing substring test_sub = "Geeks" # printing original string print("The original string is : " + test_str) # printing substring print("The substring to find : " + test_sub) # using list comprehension + startswith() # All occurrences of substring in string res = [i for i in range(len(test_str)) if test_str.startswith(test_sub, i)] # printing result print("The start indices of the substrings are : " + str(res)) |
The original string is : GeeksforGeeks is best for Geeks The substring to find : Geeks The start indices of the substrings are : [0, 8, 26]
Method #2 : Using re.finditer()
The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them.
# Python3 code to demonstrate working of # All occurrences of substring in string # Using re.finditer() import re # initializing string test_str = "GeeksforGeeks is best for Geeks" # initializing substring test_sub = "Geeks" # printing original string print("The original string is : " + test_str) # printing substring print("The substring to find : " + test_sub) # using re.finditer() # All occurrences of substring in string res = [i.start() for i in re.finditer(test_sub, test_str)] # printing result print("The start indices of the substrings are : " + str(res)) |
The original string is : GeeksforGeeks is best for Geeks The substring to find : Geeks The start indices of the substrings are : [0, 8, 26]
Recommended Posts:
- Python | Get the starting index for all occurrences of given substring
- Python | Count occurrences of a character in string
- Python | Get the string after occurrence of given substring
- Python | Frequency of substring in given string
- Python | Remove the given substring from end of string
- Python | Check if a Substring is Present in a Given String
- Python | Check if substring present in string
- Python | Get the substring from given string using list slicing
- Python | Count overlapping substring in a given string
- Python | Ways to count number of substring in string
- Python | Ways to find nth occurrence of substring in a string
- Count occurrences of a word in string
- Count occurrences of a sub-string with one variable character
- Python | Deleting all occurrences of character
- Python | Count occurrences of an element in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



