range of characters?
Hans Nowak
hans at zephyrfalcon.org
Tue Dec 16 15:19:45 EST 2003
Bud P. Bruegger wrote:
> I suppose there should be an elegant way of getting a range of
> characters as in
>
> range('A', 'Z')
>
> Anyone knows how to do that? The best I thought of so far is something
> like
> [chr(i) for i in range(ord('A'), ord('Z')+1)], but that seems overly
> verbose..
It's not built-in, but as you already demonstrated, it's easy to write your own
code...
>>> def chrange(char1, char2):
... return [chr(i) for i in range(ord(char1), ord(char2)+1)]
...
>>> chrange('a', 'd')
['a', 'b', 'c', 'd']
>>> chrange('0', '9')
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
--
Hans (hans at zephyrfalcon.org)
http://zephyrfalcon.org/
More information about the Python-list
mailing list