Python — How To Find All Occurrences in String

thecodeteacher
3 min readJan 27, 2022

--

Python — How To Find All Occurrences in String

A substring in Python is a cluster of characters that occurs within another string. Dealing with substrings can often be troublesome. One such problem is finding all the occurrences of a substring within a particular string.

This tutorial will discuss different methods to find all occurrences of a substring within a string in Python.

Use the string.count() Function to Find All Occurrences of a Substring in a String in Python

The string.count() is an in-built function in Python that returns the quantity or number of occurrences of a substring in a given particular string. Moreover, it has additional parameters start and end to specify the indices of starting and ending positions.

The count() method traverses the string and returns the number of times a specific substring has occurred in the string.

The following code uses the string.count() function to find all occurrences of a substring in a string.

#defining string and substring str1 = "This dress looks good; you have good taste in clothes." substr = "good"  #occurrence of word 'good' in whole string count1 = str1.count(substr) print(count1)  #occurrence of word 'good' from index 0 to 25 count2 = str1.count(substr,0,25) print(count2)

Output:

2 1

It is an easy method and works in every case. The only downfall of this method is that it does not return the different indices at which the substring occurs in the string.

Use List Comprehension and startswith() to Find All Occurrences of a Substring in a String in Python

This method needs two things: list comprehension and the startswith() method.

The startswith() function plays out the task of getting the beginning indices of the substring, and list comprehension is utilized to iterate through the complete target string.

The following code uses list comprehension and startswith() to find all occurrences of a substring in a string.

# defining string  str1 = "This dress looks good; you have good taste in clothes."    # defining substring substr = "good"    # printing original string  print("The original string is : " + str1)    # printing substring  print("The substring to find : " + substr)    # using list comprehension + startswith() # All occurrences of substring in string  res = [i for i in range(len(str1)) if str1.startswith(substr, i)]    # printing result  print("The start indices of the substrings are : " + str(res))

Output:

The original string is : This dress looks good; you have good taste in clothes. The substring to find : good The start indices of the substrings are : [17, 34]

Use the re.finditer() to Find All Occurrences of a Substring in a String in Python

re.finditer() is a function of the regex library that Python provides for programmers to use in their code. It helps in performing the task of finding the occurrence of a particular pattern in a string. To use this function, we need to import the regex library re first.

re.finditer() uses the pattern and string parameter in its syntax. In this case, the pattern refers to the substring.

The following code uses the re.finditer() function to find all occurrences of a substring in a string.

import re    # defining string   str1 = "This dress looks good; you have good taste in clothes."   #defining substring  substr = "good"   print("The original string is: " + str1)    print("The substring to find: " + substr)    result = [_.start() for _ in re.finditer(substr, str1)]    print("The start indices of the substrings are : " + str(result))

Output:

The original string is: This dress looks good; you have good taste in clothes. The substring to find: good The start indices of the substrings are : [17, 34]

thecodeteacher.com

codeprofesseur

--

--