Character Sequence Generation

Pedro Werneck pedro.werneck at terra.com.br
Fri Sep 23 06:23:57 CEST 2005


On Thu, 22 Sep 2005 23:26:58 -0400
Jeff Schwab <jeffrey.schwab at rcn.com> 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' .

If you want arbitrary sequences, you may use something like:


>>> [chr(x) for x in xrange(ord('a'), ord('z') + 1)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

>>> [chr(x) for x in xrange(ord('d'), ord('p') + 1)]
['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']

>>> [chr(x) for x in xrange(ord('A'), ord('Z') + 1)]
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

etc... 

-- 
Pedro Werneck



More information about the Python-list mailing list