Is there a Python library that contains a list of all ascii characters?

Something like below:

import ascii print ascii.charlist() 

To return something like [A, B, C, D ...]

+53
python ascii
May 05 '11 at a.m.
source share
6 answers

string constants may be what you want. ( docs )

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

If you want all printable characters:

 >>> string.printable
 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ! "# $% & \' () * +, -. /:;? @ [\\] ^ _` {|} ~ \ t \ n \ r \ x0b \ x0c '
+99
May 05 '11 at a.m.
source share

There he is:

 [chr(i) for i in xrange(127)] 
+19
May 05 '11 at 12:47 a.m.
source share

ASCII defines 128 characters whose byte values ​​range from 0 to 127 inclusive. Thus, to get a string of all ASCII characters, you can simply do

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

Only some of them can be printed, however, printable ASCII characters can be accessed in Python via

 import string string.printable 
+9
May 05 '11 at
source share
 for i in range(0,128): print chr(i) 

Try it!

+2
May 05 '11 at
source share

Since ASCII characters for printing are a fairly small list (bytes with values ​​from 32 to 127), they are quite easy to create when you need:

 >>> for c in (chr(i) for i in range(32,127)): ... print c ... ! " # $ % ... # a few lines removed :) y z { | } ~ 
+2
May 05 '11 at
source share

No, no, but you can easily do this:

  #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 
-3
Mar 22 '14 at 19:58
source share



All Articles