Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a list of all ASCII characters in python's standard library? [duplicate]

Tags:

Is there a field or a function that would return all ASCII characters in python's standard library?

like image 866
bzrr Avatar asked Jul 29 '13 04:07

bzrr


People also ask

How many ASCII characters are there in Python?

The entire ASCII table contains 128 characters.

How many characters are stored in the standard ASCII set?

ASCII is a 7-bit code, representing 128 different characters. When an ascii character is stored in a byte the most significant bit is always zero. Sometimes the extra bit is used to indicate that the byte is not an ASCII character, but is a graphics symbol, however this is not defined by ASCII.

How many ASCII characters are there?

ASCII is a 7-bit code - one bit (binary digit) is a single switch that can be on or off, zero or one. Character sets used today in the US are generally 8-bit sets with 256 different characters, effectively doubling the ASCII set. One bit can have 2 possible states.


2 Answers

You can make one.

ASCII = ''.join(chr(x) for x in range(128)) 

If you need to check for membership, there are other ways to do it:

if c in ASCII:     # c is an ASCII character  if c <= '\x7f':     # c is an ASCII character 

If you want to check that an entire string is ASCII:

def is_ascii(s):     """Returns True if a string is ASCII, False otherwise."""     try:         s.encode('ASCII')         return True     except UnicodeEncodeError:         return False 
like image 124
Dietrich Epp Avatar answered Sep 19 '22 10:09

Dietrich Epp


You can use the string module:

import string print string.printable 

which gives:

'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' 
like image 22
jh314 Avatar answered Sep 17 '22 10:09

jh314