Encoding strings – creating ASCII and UTF-8 bytes
Our computer files are bytes. When we upload or download from the internet, the communication works in bytes. A byte only has 256 distinct values. Our Python characters are Unicode. There are a lot more than 256 Unicode characters.
How do we map Unicode characters to bytes to write to a file or for transmission?
Getting ready
Historically, a character occupied 1 byte. Python leverages the old ASCII encoding scheme for bytes; this sometimes leads to confusion between bytes and proper strings of Unicode characters.
Unicode characters are encoded into sequences of bytes. There are a number of standardized encodings and a number of non-standard encodings.
Plus, there also are some encodings that only work for a small subset of Unicode characters. We try to avoid these, but there are some situations where we'll need to use a subset encoding scheme.
Unless we have a really good reason not to, we almost...