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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

The original string is : GeeksforGeeks is best for Geeks
The substring to find : Geeks
The start indices of the substrings are : [0, 8, 26]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.