Character Sets
Everything a computer stores or processes is a binary pattern, a sequence of 0s and 1s. Text is no different: a computer does not store the shapes of letters, so the letters, digits and symbols you type have to be turned into binary patterns before they can be saved, sent or printed.
A character set is a defined collection of characters that a computer can represent. It lists every character the system is able to handle: letters such as A and q, digits such as 4, punctuation such as ! and ?, and usually the space character too. Anything that is missing from the set cannot be represented at all, so the collection has to be chosen to cover everything the computer is expected to work with. Real character sets such as ASCII and Unicode work in exactly this way.
Within the character set, each character is mapped to a binary code. The codes must be unique, meaning no two characters share the same pattern, because a pattern that could mean two different characters could never be decoded reliably. Here is a made-up character set for a lift display that can only show six characters:
| Character | Binary code |
|---|---|
| G | 000 |
| R | 001 |
| 2 | 010 |
| ? | 011 |
| (space) | 100 |
| # | 101 |
When the display is sent 010, the only possible match is 2, so it never shows the wrong thing. If both 2 and ? were stored as 011, the display would have no way of knowing which one you meant and the message would come out garbled. Punctuation and spaces need codes for the same reason: they are characters that have to be stored, so they cannot be left out of the set.
Python can move between a character and its code: ord() returns the code for a character and chr() turns a code back into a character. Notice that each different character gives a different number, which is what makes the mapping unique.
Edexcel GCSE Computer Science · Character Sets