liveBook · Manning

ASCII

As explained in The Quick Python Book, Fourth Edition

ASCII is a character-encoding standard that assigns numeric codes to a defined set of English letters, digits, punctuation marks, and control characters.

ASCII is a 7-bit character-encoding standard with 128 values, including English letters, digits, punctuation, and control characters. Only 95 values are printable. It remains a common lowest denominator for data exchange, but it cannot represent most writing systems or many accented characters. section 21.2.1

Python exposes ASCII characters through ordinary strings and numeric escape sequences:

  • Octal escapes use a backslash followed by three octal digits, such as '\155'. section 6.3.2
  • Hexadecimal escapes begin with '\x' and continue through hexadecimal digits, such as '\x6D'. section 6.3.2
  • Both forms can represent nonprintable ASCII characters, such as newline: '\012' or '\x0A'. section 6.3.2

ASCII bytes versus broader text

A file containing bytes [65, 66, 67, 255, 192, 193] may display as 'ABCÿÀÁ' under one interpretation. Reading it as UTF-8 raises UnicodeDecodeError because bytes outside the ASCII range do not identify their encoding by themselves. UTF-8 preserves basic ASCII while supporting a much larger character set. section 21.2.1

'm'
 
'm'
 
'\155'
 
'm'
 
'\x6D'
 
'm'

Compare three spellings of the same ASCII character: a literal, an octal escape, and a hexadecimal escape. section 6.3.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines 7-bit ASCII, printable range, numeric escapes, and encoding limits; stops short of explaining Unicode internals.
Examples
Demonstrates equivalent escapes for m, newline representations, and decoding files containing bytes outside the ASCII range.
What it assumes
Assumes basic programming and file-handling knowledge while defining ASCII’s range and its relationship to Unicode.
What you can do after
Diagnose ASCII-versus-UTF-8 decoding problems and write ASCII characters using literal, octal, or hexadecimal forms.
How it connects
Connects ASCII with Unicode, UTF-8, character encoding, text files, and escape sequences.

Also explained in

Compare how 2 books explain ASCII →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.