Character Sequence Generation

Peter Otten __peter__ at web.de
Fri Sep 23 02:45:38 EDT 2005


Jeff Schwab wrote:

> What's the best way to generate a sequence of characters in Python?  I'm
> looking for something like this Perl code: 'a' .. 'z' .

>>> import re
>>> def char_set(a_z, all_chars="".join(map(chr, range(256)))):
...     return re.compile("[%s]" % a_z).findall(all_chars)
...
>>> for c in char_set("a-f0-9"): print c,
...
0 1 2 3 4 5 6 7 8 9 a b c d e f
>>> for c in char_set("\s"): print repr(c),
...
'\t' '\n' '\x0b' '\x0c' '\r' ' '

Peter




More information about the Python-list mailing list