Find all occurrences of substring in string Python

In this post, we will learn how to find all occurrences of substring in string Python with examples or find all occurrences indices of a substring in String Python.We will use count(),Regex Module,while loop and list Comprehension and Find() methods and stratwith() method

1.stratswith() with list Copreshesion to Find all occurrences of substring in string Python


The startswith() method is used to get starting index of a substring in the original string. The list comprehension is to iterate over the element of the list and return the index of a substring in the original string.

original_Str = "Devenum.com is Devenum Tahnks Devenum on Devenum"
  
#occurrence substring to find
sub_Str = "Devenum"


res = [i for i in range(len(original_Str)) if original_Str.startswith(sub_Str, i)]

print("the indices of sub_str : " + str(res))

Output

the indices of sub_str : [0, 15, 30, 41]

2. Find all occurrences of substring in string Python using Regex


In this example, we have used library regex and its finall() method returns the occurrence of a substring from the original string. The len() function returns the count of the number of times the substring occurrence in the original string.

  • Import re module
  • Use re.compile() to set regular expression
  • The findall() method to find all occurrences of substring index in the original string.
import re
original_Str = "Devenum.com is Devenum Thanks Devenum on Devenum"
  
#occurrence substring to find
sub_Str = regexPattern = re.compile("Devenum")


res_occurrence  = regexPattern.findall(original_Str)

print("The 'Devenum' occurrence in original string: " + str(res_occurrence))
print("count of 'Devenum' occurrence in original string: " , len(res_occurrence))




Output

The 'Devenum' occurrence in original string: ['Devenum', 'Devenum', 'Devenum', 'Devenum']
count of 'Devenum' occurrence in original string:  4

3. count() to find all occurrences of substring in string Python


The Python string class count method counts the occurrence of a substring in an original string without overlapping. It looks for a substring in the original string and returns its count.

original_Str = "Devenum.com is Devenum Thanks Devenum on Devenum"
  
#occurrence substring to find
count = original_Str.count('Devenum')

print("The 'Devenum' occurrence in original string: ",count)

Output

The 'Devenum' occurrence in original string:  4

4. re.finditer to Find all occurrence of substring in string Python


In this example, we have used library regex and its finditer() along with _.start() that returns the occurrence of a substring from the original string. The list comprehension is used to iterate over the string and returns the start index of a substring in the original string

import re
original_Str = "Devenum.com is Devenum Tahnks Devenum on Devenum"
  
#occurrence substring to find
sub_Str = 'Devenum'

result = [_.start() for _ in re.finditer(sub_Str, original_Str)]

print("The 'Devenum' occurrence in original string: ",result)

Output

The 'Devenum' occurrence in original string:  [0, 15, 30, 41]

5. Find all overlap occurrence of substring in string Python


In this python program, we are iterating over the original_Str by using the while loop and looking for sub_Str by using the string class find() method that returns the index of sub_str in the original string and appends into res_list, when the loop will over it returns a list that contains an index of a substring in the original string.

original_Str = "Devenum.com is DevenumDevenumDevenum Tahnks Devenum on Devenum"
  
#occurrence substring to find
sub_Str = 'Devenum'

res_list = []
pos = original_Str.find(sub_Str)

while pos != -1:
    res_list.append(pos)
    pos = original_Str.find(sub_Str, pos + 1)

print("The 'Devenum' occurrence in original string: ",res_list)

Output

The 'Devenum' occurrence in original string:  [0, 15, 22, 29, 44, 55]