To get all substrings of a string in Python, the easiest way is to use list comprehension and slicing.

string = "example"

all_substrings = [string[i:j] for i in range(len(string)) for j in range(i + 1, len(string) + 1)]

print(all_substrings)

#Output:
['e', 'ex', 'exa', 'exam', 'examp', 'exampl', 'example',
 'x', 'xa', 'xam', 'xamp', 'xampl', 'xample', 'a', 'am', 'amp', 'ampl', 'ample', 
 'm', 'mp', 'mpl', 'mple', 'p', 'pl', 'ple', 'l', 'le', 'e']

You can also use list comprehension with the itertools module combinations() function to get a list of all substrings of a string.

from itertools import combinations

string = "example"

all_substrings = [string[x:y] for x, y in combinations(range(len(string) + 1), r=2)]

print(all_substrings)

#Output:
['e', 'ex', 'exa', 'exam', 'examp', 'exampl', 'example',
 'x', 'xa', 'xam', 'xamp', 'xampl', 'xample', 'a', 'am', 'amp', 'ampl', 'ample', 
 'm', 'mp', 'mpl', 'mple', 'p', 'pl', 'ple', 'l', 'le', 'e']

When working with string variables, the ability to easily create new objects from those strings can be very useful.

One such case is if you want to get all substrings of a string variable in your Python code.

To get all substrings of a string in Python, the easiest way is with list comprehension and string slicing.

With list comprehension, we will loop over all combinations of positions of our string and get all of the substrings using slicing.

Below is a simple example showing how you can get all substrings of a string in Python.

string = "example"

all_substrings = [string[i:j] for i in range(len(string)) for j in range(i + 1, len(string) + 1)]

print(all_substrings)

#Output:
['e', 'ex', 'exa', 'exam', 'examp', 'exampl', 'example',
 'x', 'xa', 'xam', 'xamp', 'xampl', 'xample', 'a', 'am', 'amp', 'ampl', 'ample', 
 'm', 'mp', 'mpl', 'mple', 'p', 'pl', 'ple', 'l', 'le', 'e']

Using itertools Module combinations() Function to Get All Substrings of a String in Python

There is another way you can get all substrings of a string in Python. The itertools module has many great functions which allow us to produce complex iterators.

The itertools module combinations() function can help us create an iterator of all possible index positions of a starting point and ending point for each substring.

With combinations(), we will have all of the possible substring starting point and ending points, and will be able to get all of the substrings of a string.

Below is another example of getting every substring of a string in Python with the help of the combinations() function.

from itertools import combinations

string = "example"

all_substrings = [string[i:j] for i, j in combinations(range(len(string) + 1), r=2)]

print(all_substrings)

#Output:
['e', 'ex', 'exa', 'exam', 'examp', 'exampl', 'example',
 'x', 'xa', 'xam', 'xamp', 'xampl', 'xample', 'a', 'am', 'amp', 'ampl', 'ample', 
 'm', 'mp', 'mpl', 'mple', 'p', 'pl', 'ple', 'l', 'le', 'e']

Hopefully this article has been useful for you to learn how to get all substrings of a string variable in your Python code.

Categorized in:

Python,

Last Update: March 14, 2024

Tagged in: