# get all permutations of string import itertools for p in itertools.permutations('123'): print(p) # ( ' 1 ', ' 2 ', ' 3 ') ( ' 1 ' , ' 3 ', ' 2 ' ) ( ' 2 ', ' 1 ', ' 3 ' )