How do I get a list of all the ASCII characters using Python?
Question:
I’m looking for something like the following:
import ascii
print(ascii.charlist())
Which would return something like ["A", "B", "C", "D" ... ]
.
Answers:
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!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c'
For every single character defined in the ASCII standard, use chr
:
>>> ''.join(chr(i) for i in range(128))
'x00x01x02x03x04x05x06x07x08tnx0bx0crx0ex0fx10x11x12x13x14x15x16x17x18x19x1ax1bx1cx1dx1ex1f !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~x7f'
for i in range(0, 128):
print(chr(i))
Here it is:
[chr(i) for i in range(128)]
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
{
|
}
~
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
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
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()
.
I’m looking for something like the following:
import ascii
print(ascii.charlist())
Which would return something like ["A", "B", "C", "D" ... ]
.
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!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ tnrx0bx0c'
For every single character defined in the ASCII standard, use chr
:
>>> ''.join(chr(i) for i in range(128))
'x00x01x02x03x04x05x06x07x08tnx0bx0crx0ex0fx10x11x12x13x14x15x16x17x18x19x1ax1bx1cx1dx1ex1f !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~x7f'
for i in range(0, 128):
print(chr(i))
Here it is:
[chr(i) for i in range(128)]
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
{
|
}
~
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
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
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()
.