print a ... z, A ... Z, "\n"' in Python

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sat Mar 3 04:11:49 EST 2007


js:
> crange = lambda c1, c2: [ chr(c) for c in range(ord(c1), ord(c2)+1) ]
> ''.join(chr(c) for c in crange('a', 'z') + crange('A', 'Z'))

Yes, managing char ranges is a bit of pain with Python.
You can also pack those chars:

xcrange = lambda cc: (chr(c) for c in xrange(ord(cc[0]), ord(cc[1])
+1))

But I don't like that much.

The calls:
''.join(xcrange('az')) + ''.join(xcrange('AZ'))

But note that you return the last item of the range too, and that goes
against the semantic of the usual Python range/xrange, so you may want
to call this function with another name.

Maybe there are better ways to solve this problem. Maybe a way to
generate (closed?) char ranges can be added to the Python standard
lib.

Bye,
bearophile




More information about the Python-list mailing list