Chapter 13
Recall from Chapters 5 and 9 that both integers and floating-point numbers are stored in binary, although the exact format for each is different. Binary is used because computer memory is essentially a sequence of on/off switches, and these can easily be thought of as 0’s and 1’s.
This raises the question: how is text stored in memory? The basic idea is simply to assign a code number to each character. Then, for each character, we just store the corresponding code number in binary.
The standard coding system for the English alphabet is ASCII, pronounced “ask-ee.” Standard ASCII codes are seven bits long, although each character usually occupies eight bits because that makes a complete byte. There has been disagreement about how to use that last eighth bit; however, more recently, Unicode is becoming the standard for international communication. It is a two-byte code that extends ASCII.
One of the features of ASCII is that digits and letters appear sequentially:
0–9 |
Codes 48–57 |
A–Z |
Codes 65–90 |
a–z |
Codes 97–122 |
Python offers two built-in functions that allow you to work directly with ASCII codes:
chr(n) |
Character with ASCII code n. |
ord(c) |
ASCII code for the character c. |
18.116.87.196