• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Get All Substrings of a String in Python

Get All Substrings of a String in Python

May 8, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Remove Parentheses from String Using Python
  • 2.  Replace Values in Dictionary in Python
  • 3.  Truncate String in Python with Slicing
  • 4.  Create Empty String Variable in Python
  • 5.  How to Hide Turtle in Python with hideturtle() Function
  • 6.  Symmetric Difference of Two Sets in Python
  • 7.  Using Python to Sort Two Lists Together
  • 8.  Reverse String with Recursion in Python
  • 9.  pandas DataFrame size – Get Number of Elements in DataFrame or Series
  • 10.  Python Get Number of Cores Using os cpu_count() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy