To get all possible substrings of a string in Python, you can use nested loops to iterate through each combination of start and end indices.
In this example,
str
with a value of 'Hello'
.substrings
to store all substrings.substrings
list.substrings
list.str = 'Hello'
substrings = []
for i in range(len(str)):
for j in range(i + 1, len(str) + 1):
substrings.append(str[i:j])
print('All substrings:', substrings)
All substrings: ['H', 'He', 'Hel', 'Hell', 'Hello', 'e', 'el', 'ell', 'ello', 'l', 'll', 'llo', 'l', 'lo', 'o']
In this tutorial, we learned How to get all the possible Substrings of a String in Python language with well detailed examples.