Table of Contents
Dealing with and manipulating strings is a fundamental part of understanding the basics of Python, and the use of substrings and strings can be seen in essentially every Python program. This tutorial focuses on one such part of substrings. The article demonstrates the different approaches available to find all substrings of string in Python.
what Is a String in Python?
A string is a collection or a sequence of characters stored in a variable, and is usually enclosed within single quotes or double-quotes. It is one of the most versatile data types in Python, as it is capable of storing anything, whether it is a character or a number.
what Is a Substring?
A substring can be defined as any sequence of characters that are contained within a string. In simple terms, it can be said to be a slice or a small part of the original string.
How To Find All Substrings of String in Python?
There are mainly three methods to implement the task at hand, with all of these approaches being drastically dissimilar to one another.
Using a User-Defined Function that Makes Use of String Slicing to Find All Substrings of String in Python.
We can simply create a user-defined function that makes use of string slicing to implement the task of finding all substrings of string in Python.
List slicing is a general practice that is widely utilized while dealing with lists in Python, as it provides users the ability to manipulate strings efficiently.
With list slicing, we can emphasize the portion of the string that we would want to get, and it specifically returns that part or portion of the list. Based on this, there is another term known as string slicing, which implements the same idea on a string, which is what we will implement by using this approach.
The following code uses a user-defined function that makes use of string slicing to find all substrings of string in Python.
1 2 3 4 5 6 7 8 9 10 |
def allsubs(string): length = len(string) lt = [] for x in range(length): for y in range(x,length): lt.append(string[x:y + 1]) return lt print (allsubs('java2blog')) |
The above code provides the following output:
Using List Comprehension Along with String Slicing to Find All Substrings of String in Python.
The same approach mentioned above can be utilized along with using list comprehension in Python.
List comprehension provides a compact way of creating a list when values are to be taken from an already existing list. The list comprehension code is generally a one-liner and provides conciseness to the Python code. Moreover, it also eliminates the need to manually utilize the for
loop across multiple lines to define the process of creating another list.
Apart from making the code smaller, it also decreases the compile time of the code, which makes it a useful asset when writing large complex codes.
The following code uses list comprehension along with string slicing to find all substrings of string in Python.
1 2 3 4 5 6 |
def allsubs(x): length = len(x) return [x[i:j+1] for i in range(length) for j in range(i,length)] print (allsubs('java2blog')) |
The above code provides the following output:
Using the itertools.Combinations()
Function to Find All Substrings of String in Python.
The combinations()
function, which is a built-in
function in the itertools
library, can be utilized to implement the task to find all substrings of string in Python.
The term itertools.combinations()
is broadly categorized under the term Combinatoric Generators
and is highly utilized in the process of simplification of cartesian products, permutations, and combinations.
The itertools.combinations()
, in our case, provides all the possible sequences of characters that can be utilized and referred to as a substring of the original string. The original string, in this case, would be our iterator.
The following code uses the itertools.combinations()
function to find all substrings of string in Python.
1 2 3 4 5 6 7 |
from itertools import combinations a = 'java2blog' result = [a[x:y] for x, y in combinations( range(len(a) + 1), r = 2)] print(str(result)) |
The above code provides the following output:
That’s all about how to find all substrings of String in Python.