Fix Python – How do I get a list of all the ASCII characters using Python?

Question

Asked By – rectangletangle

I’m looking for something like the following:

import ascii

print(ascii.charlist())

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

Now we will see solution for issue: How do I get a list of all the ASCII characters using Python?


Answer

The constants in the string module may be what you want.

All ASCII capital letters:

>>> import string
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

All printable ASCII characters:

>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'

For every single character defined in the ASCII standard, use chr:

>>> ''.join(chr(i) for i in range(128))
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'

This question is answered By – Acorn

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0