python 2.7 word generator
<p>Algorithm: take input on how many letters to go back, for loop to loop a-z, lock the first character, loop the second character, lock the first two, loop the third, and so on and so forth. The out put will look like a, b, c, d... aa, ab, ac, ad... aaa, aab, aac... and so on. I am very new to python. I have something that cycles through the alphabet, but my problem is to lock the first and cycle the second and so on.</p>

<pre><code>w = 'abcdefghijklmnopqrstuvwxyz'
n = input ("# of characters: ")

for a in range(0,n):
        for i in w:
                print i
</code></pre>

<p>To avoid huge RAM requirements, use <a href="https://docs.python.org/2/library/itertools.html#itertools.combinations" rel="nofollow"><code>itertools.combinations</code></a> to generate a single output at a time, handling the "locking" for you:</p>

<pre><code>from future_builtins import map  # Not needed on Py3, only on Py2
from itertools import combinations

w = 'abcdefghijklmnopqrstuvwxyz'
# Don't use input on Py2; it's an implicit eval, which is terrible
# raw_input gets str, and you can explicitly convert to int
n = int(raw_input("# of characters: "))

# You want 1-n inclusive on both ends, not 0-n exclusive at the end, so tweak range
for wdlen in xrange(1, n+1):
    # Generator based map with ''.join efficiently converts the tuples
    # from combinations to str as you iterate
    for wd in map(''.join, combinations(w, wdlen)):
        print wd
</code></pre>

python python-2.7