Problem:
Given a string, e.g. foobar
, you want to get the list of all suffixes of said string, e.g. ["r", "ar", "bar", "obar", "oobar", "foobar"]
Solution:
Use this snippet:
def all_suffixes(s): return [s[-i:] for i in range(1, len(s) + 1)] s = "foobar" print(all_suffixes(s)) # ['r', 'ar', 'bar', 'obar', 'oobar', 'foobar']