import itertools def all_permutations_substrings(a_str): return ( ''.join(item) for length in xrange(1, len(a_str)+1) for item in itertools.permutations(a_str, length))
Note, however, that these are true permutations - as in, hello will have any substring that has two l in it twice, since l will be considered "unique". If you want to get rid of this, you can pass it through set() :
all_permutations_no_dupes = set(all_permutations_substrings(a_str))
Amber source share