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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • 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.  Using Python to Count Number of Lines in String
  • 2.  Using Python to Remove First Character from String
  • 3.  Create Empty File with Python
  • 4.  How to Find the Longest String in List in Python
  • 5.  Using Python to Append Character to String Variable
  • 6.  Remove Trailing Zeros from String with rstrip() in Python
  • 7.  Python Random Uniform – Generate Random Numbers Uniformly in Range
  • 8.  Get Python Dictionary Keys as List
  • 9.  Length of Set Python – Get Set Length with Python len() Function
  • 10.  Check if String is Date in Python

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy