Incrementing a string

Jeffrey Froman jeffrey at fro.man
Thu Sep 16 00:20:03 EDT 2004


John Velman wrote:

> $label++;
> 
> This conveniently steps through the alphabet, then goes on to aa, ab,ac,
> ...
> 
> In Python I can get from a to z with a generator as so:
[snip]
> But it looks like going beyond z to aa and so on is (relatively)
> complicated.

Here's one way, perhaps it is simple enough for you:

import string

def strpp(s):
    for i in s:
        yield i
    for i in strpp(s):
        for j in s:
            yield i + j

labels = strpp(string.lowercase)
label = label.next() # ad infinitum


Hope you enjoy Python,
Jeffrey



More information about the Python-list mailing list