Find all substrings of a string python


In this article, we will learn 3 different methods to find all substrings of a string Python. We will look at examples and their code with proper explanations.

A substring is a part of a string that is not separated by any other string. For example, if we have a string "abcde" then "abc", "a", "de", "bcd", etc are all substrings of "abcde" but "ace", "abd", "ade", etc are not.

Let's extract all substrings of a string in Python.

    Table of Contents

  1. Using for loop
  2. Using list comprehension
  3. Using itertools.combinations() function
  4. Conclusion
find all substrings of a string python

1. Using for loop

The simplest way to find all substrings of a string is by using for loop in Python.

Algorithm:

  1. Take your string and create an empty list to store all substrings.
  2. Create a for loop that will run from 0 to the length of the string with variable i.
  3. Inside the for loop, create another for loop that will run from i+1 to the length of the string, having variable j.
  4. Use the slice operator (str[i:j]) to get the substring between i and j.
  5. Append the substring to the list.
  6. Depending on the taken string, you may have duplicates in the list. Remove duplicates using list function list(set(substr)).

Here is the code:

str = "Python"
        
substr = []
for i in range(len(str)):
    for j in range(i+1, len(str)+1):
        substr.append(str[i:j])

# remove duplicates
substr = list(set(substr))
print(substr)

Output

['P', 'Py', 'Pyt', 'Pyth', 'Pytho', 'Python', 'y', 'yt', 'yth', 'ytho', 'ython', 't', 'th', 'tho', 'thon', 'h', 'ho', 'hon', 'o', 'on', 'n']

Code explanation: External loop will run from 0 to 6 (len(str)=6). Taking i=0 when the loop enters the loop, we have j=1. And inside this internal loop use the slice operator to get the substring between i and j which is str[0:1] which is "P". The second time j=2 and the substring is str[0:2] which is "Py". And so on.

This way we have all substrings of the string.


2. Using list comprehension

List comprehension is a short way to create a list in Python. It is a list of elements for which we have to write a condition.

The same logic used above can be applied here but in the form of list comprehension.

Let's see the example code to understand how can we find all substrings.

str = "Python"

# list comprehension
substr = [str[i:j] for i in range(len(str)) for j in range(i+1, len(str)+1)]

# remove duplicates if any
substr = list(set(substr))

print(substr)

Output

['P', 'Py', 'Pyt', 'Pyth', 'Pytho', 'Python', 'y', 'yt', 'yth', 'ytho', 'ython', 't', 'th', 'tho', 'thon', 'h', 'ho', 'hon', 'o', 'on', 'n']

Code explanation: The working concept is very much the same as the above example.


Using itertools.combinations() function

itertools is a Python module that contains a number of functions that are useful in various areas of mathematics and programming.

One such function inside this module is the combinations() function. It creates all possible combinations of any iterable object and returns them as a list of tuples.

For example, combinations(range(4), r = 2) will return [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)].

We can use this property of combinations functions to get all combinations of substrings of a string.

Here is an example to demonstrate how to use the combinations() function to get all combinations of substrings of a string.

# import combinations function
from itertools import combinations

str = "Python"

# Get all substrings of string
result = [str[i:j] for i, j in combinations(range(len(str) + 1), 2)]

# printing result
print(result)

Output

['P', 'Py', 'Pyt', 'Pyth', 'Pytho', 'Python', 'y', 'yt', 'yth', 'ytho', 'ython', 't', 'th', 'tho', 'thon', 'h', 'ho', 'hon', 'o', 'on', 'n']

Code explanation: As we have seen above the 'combinations' function return list of tuples. So here return value would be [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (1, 2),…,(5,6)]. From these tuples we can get all substrings of the string.


Conclusion

We learned 3 different ways to find all substrings of a string Python. The easiest one is using for loop and slice operator. But we can also use list comprehension and itertools.combinations() function to find all substrings of a string.