Instagram
youtube
Facebook
  • 1 year, 4 months ago
  • 886 Views

How to create all substring combination from a string

Mradul Mishra
Table of Contents

Hello all in this tutorial we are going to learn how can we create combinations substrings for a string.

In python to create a substring from a string we will use the following steps:

  1. We will be using list comprehension in this tutorial to solve the problem.
  2. In the code we have defined two loops.
  3. The first loop will iterate in the range from 0 to length of string. In this way we will be able to get all the letters in the word one by one.
  4. In the second loop or you can cal, or the inner loop,rating from (i + 1) to length of string. Where i is the index value we are getting from the first loop.
  5. In this way lets say if we have a string "aac" we will be able to create list substrings like 
    ['a', 'a', 'aa', 'aac', 'ac', 'c'].

 

Code to create a substring from a string in Python

def findStrings(w):
    # Write your code here
    for val in w:
        res = [val[i:j] for i in range(len(val)) for j in range(i+1, len(val) + 1)]
    return res

word = input("Enter the String Here: ")
out = findString(word)
print(out)

 

Add a comment: