generating a liste of characters

Mark Tolonen metolone+gmane at
Fri Dec 5 14:56:34 CET 2008

<bearophileHUGS at> wrote in message 
news:df7b75eb-97bf-4538-880b-f65b5ed992ff at
>Mark Tolonen:
>> Writing a helper function reduces code repetition and improves 
>> readability:
>> def crange(startch,endch):
>> '''Return a list of characters from startch to endch, inclusive.'''
>> return [chr(c) for c in xrange(ord(startch),ord(endch)+1)]
>In Python ranges are open on the right, so I name cinterval such

Yes, and that's fine when dealing with integers and slicing, but when 
dealing with characters, it is non-obvious what character to use.  What 
"looks" correct?

   chars = crange('0','9') + crange('A','Z') + crange('a','z') # inclusive


   chars = crange('0',':') + crange('A','[') + crange('a','{')  # open right

or if you didn't know the character after the one you actually wanted:

   chars = crange('0',chr(ord('9')+1)) + crange('A',chr(ord('Z')+1)) + 
crange('a',chr(ord('z')+1))  # open right, but messy


More information about the Python-list mailing list