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:
- We will be using list comprehension in this tutorial to solve the problem.
- In the code we have defined two loops.
- 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.
- 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.
- 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: