Find All Occurrences of a Substring in a String in Python

Hello coders!! In this article, we will explore methods in Python to find all occurrences in a string. To make the concept clear, we will be looking into a detailed illustration of the code to reach the required output.

What is a substring?

A substring in Python is a sequence of characters presents within another string. For example, consider a string abaabaabab. Here, aab is a substring occurring twice in the string. Also, aba is yet another substring occurring thrice in the string.

Often, when handling strings, we may have trouble handling substrings. This includes the inconvenience of finding all positions of a particular substring in a string. In this article, we will discuss how we can handle this.

Python Code to find all occurrences in string

1)Using list comprehension + startswith() in Python to find all occurrences in a string

This functions helps in finding a given substring in the entire string or in the given portion of the string.

Syntax:

string.startswith(value, start, end)

Parameter List:

  • value: It is a mandatory field. It holds the value with which we check if the string starts with that value.
  • start: It is an optional field. It is an integer value that specifies the position from where to start the search.
  • end: It is an optional field. It is an integer value that specifies the position from where to end the search.

Return Value:

Returns the index at which the given substring is found.

string = "pythonpool for python coding"

substring = "python"

print("The original string is: " + string) 

print("The substring to find: " + substring) 

result = [i for i in range(len(string)) if string.startswith(substring, i)] 

print("The start indices of the substrings are : " + str(result))

Output & Explanation:

Using list comprehension + startswith() to find all occurrences in a string
Output

In this code, the input string was “pythonpool for python coding“. We selected the substring “python“. Using the startswith() function, we found the occurrences of the substring in the string. As a result, we found the substring in index 0 and 15.

2) Using re.finditer() in Python to find all occurrences in a string

It is a function of the regex library provided by python that helps in finding the occurrence of a certain pattern in a string.

Syntax:

re.finditer(pattern, string, flags=0)

Parameter list:

  • pattern: the pattern that needs to be matched
  • string: the string in which we find the pattern

Return value:

This function returns an iterator of non-overlapping matches for the pattern in the string.

import re 

string = "pythonpool for python coding"

substring = "python"

print("The original string is: " + string) 

print("The substring to find: " + substring) 

result = [i.start() for i in re.finditer(substring, string)] 

print("The start indices of the substrings are : " + str(result))

Output & Explanation:

Using re.finditer() in Python to find all occurrences in a string
Output

In this code, the input string was “pythonpool for python coding“. We selected the substring “python“. Using the re.finditer() function, we found the non-overlapping occurrences of the substring in the string. As a result, we found the substring in index 0 and 15.

3) Using re.findall() in Python to find all occurrences in a string

This function is used to find all the non-overlapping substring in the given string. The string is thoroughly scanned from left to right returning the matches in the same order.

Syntax:

re.finditer(pattern, string, flags=0)

Parameter list:

  • pattern: the pattern that needs to be matched
  • string: the string in which we find the pattern

Return Value:

It returns all matches of the pattern as a list of strings.

import re 

string = "pythonpool123 for python456 coding"

substring = '\d+'

print("The original string is: " + string) 

print("The substring to find: " + substring) 		
	
result = re.findall(substring, string) 
print(result) 

Output & Explanation:

Using re.findall() in Python to find all occurrences in a string
Output

In this code, the input string was “pythonpool123 for python456 coding“. We selected the substring “\d+“. Using the re.findall() function, we found the occurrences of the substring. In this case, we are looking for integers in the string. As a result, the output is a list containing all the integer values.

Conclusion: Python find all occurrences in a string

In this article, we learned the different ways of finding all the occurrences of a given substring in a string using different functions in Python.

5 2 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Alex
Alex
9 days ago

Nice job. Here’s another. Naive and without the use of modules or methods.

needle = 'aba'
haystack = 'abaabaabab'
count = 0
n_count = len(needle)
ss_count = 0


for i in haystack:
   if haystack[count:n_count] == needle:
      ss_count += 1
   count += 1
   n_count += 1

print(ss_count)
Python Pool
Admin
7 days ago
Reply to  Alex

Thank You for providing an alternative πŸ™‚