[python] Is there a Python Library that contains a list of all the ascii characters?

Something like below:

import ascii

print ascii.charlist()

Which would return something like [A, B, C, D...]

This question is related to python ascii

The answer is


Since ASCII printable characters are a pretty small list (bytes with values between 32 and 127), it's easy enough to generate when you need:

>>> for c in (chr(i) for i in range(32,127)):
...     print c
... 

!
"
#
$
%
... # a few lines removed :)
y
z
{
|
}
~

You can do this without a module:

    characters = list(map(chr, range(97,123)))

Type characters and it should print ["a","b","c", ... ,"x","y","z"]. For uppercase use:

    characters=list(map(chr,range(65,91)))

Any range (including the use of range steps) can be used for this, because it makes use of Unicode. Therefore, increase the range() to add more characters to the list.
map() calls chr() every iteration of the range().


for i in range(0,128):
    print chr(i)

Try this!


No, there isn't, but you can easily make one:

    #Your ascii.py program:
    def charlist(begin, end):
        charlist = []
        for i in range(begin, end):
            charlist.append(chr(i))
        return ''.join(charlist)

    #Python shell:
    #import ascii
    #print(ascii.charlist(50, 100))
    #Comes out as:

    #23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abc

ASCII defines 128 characters whose byte values range from 0 to 127 inclusive. So to get a string of all the ASCII characters, you could just do

''.join([chr(i) for i in range(128)])

Only some of those are printable, however- the printable ASCII characters can be accessed in Python via

import string
string.printable

Here it is:

[chr(i) for i in xrange(127)]