import itertools n = 10 iter = itertools.product('abcdefg', repeat=7) for _ in range(n-1): print('a' + ''.join(iter.next()) + 'a') print('an')