# How to write a simple python program that prints letters in ascending order?

## Question:

For example I would like to have:

a

.

.

.

z

aa

ab

.

.

.

az

bz

.

.

.

zz

aaa

and so on.

Currently I’m here but I am lost. So feel free to propose a completely different solution.

```
count = 0
string = ''
for i in range(100):
count += 1
if i % 26 == 0:
count = 0
string += 'a'
ch = 'a'
x = chr(ord(ch) + count)
string = string[:-1] + x
print(i + 1, string)
```

and my output is something like this:

1 a

2 b

.

.

.

26 z

27 za

28 zb

.

.

.

52 zz

53 zza

54 zzb

.

.

.

## Answers:

Maybe try something like the following:

range(97,123) simply creates a range of numbers from 97 to 122, which converted to ASCII equates to a…z (done using chr())

So all our FUnction does, is it recieves a base string (starts with empty), prints out the base + range of charachters and calls its self with base + every charachter as the new base and depth decremented by 1

```
def printcharachters(base, depth):
if depth > 0:
for a in range(97, 123):
print(base + chr(a))
for a in range(97, 123):
printcharachters(base + chr(a), depth - 1)
printcharachters("", 2)
```

Replace the depth with your desired depth (for 2 the last string would be zz for 4 it would be zzzz).

Using more of standard library:

```
import itertools
import string
for i in range(1, 3): # Print items of length 1 and 2
for prod in itertools.product(string.ascii_lowercase, repeat=i):
print(''.join(prod))
```

What you describe is a sorted output of n-th powers of set `{'a'...'z'}`

in terms of cartesian products in string format (cartesian power `n`

of a set `X`

is, from a simple point of view, a set of all possible tuples `(x_1, ..., x_n)`

where all `x_i`

are contained in `X`

). `itertools.product`

implements exactly cartesian product and outputs in order of presence, so just loop over them. `string.ascii_lowercase`

is a simple string containing all letters `a..z`

in natural order. This is fast (uses C implementation of `itertools`

).