135

I'm looking for something like the following:

import ascii

print(ascii.charlist())

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

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

7 Answers7

229

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'
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • Interesting that `string.printable` limits itself to ASCII characters only. And I'm not sure how many contexts would be able to properly interpret the vertical tab `\x0b`. – Mark Ransom Sep 05 '22 at 03:07
38

Here it is:

[chr(i) for i in range(128)]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Simon
  • 31,675
  • 9
  • 80
  • 92
  • 4
    For those curious, `sorted({chr(i) for i in range(128)} - set(string.printable))` is `['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', '\x7f']` – Boris Verkhovskiy Mar 12 '21 at 06:12
20

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 100 of those are considered printable. The printable ASCII characters can be accessed via

import string
string.printable
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
gsteff
  • 4,764
  • 1
  • 20
  • 17
5

Since ASCII printable characters are a pretty small list (bytes with values between 32 and 126 inclusive), 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
{
|
}
~
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
sarnold
  • 102,305
  • 22
  • 181
  • 238
4

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().

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Benj
  • 736
  • 7
  • 21
3
for i in range(0, 128):
    print(chr(i))
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
lucemia
  • 6,349
  • 5
  • 42
  • 75
-10

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
pythonprogrammer
  • 693
  • 2
  • 7
  • 7
  • 19
    Three years after [the accepted answer](https://stackoverflow.com/a/5891469/321973) clearly mentions `string.printable`, you claim "no, there isn't"? – Tobias Kienzler Aug 23 '16 at 08:35