1. A custom function can return a list of substrings in order.
def restr(s):
results = []
# x + 1 表示子串的长度
for x in range(len(s)):
# i 表示滑窗长度
for i in range(len(s) - x):
results.append(s[i:i + x + 1])
return results
print(restr("flow"))
The result is:
2. It can also be solved in one line, which is equivalent to the custom function above.
>>> s = 'flow'
>>> [s[i:i + x + 1] for x in range(len(s)) for i in range(len(s) - x)]
['f', 'l', 'o', 'w', 'fl', 'lo', 'ow', 'flo', 'low', 'flow']