Python Encodings and Code Points
PCAP objective 3.1 is understand machine representation of characters, and it is the one string objective that is really about types. A str is a sequence of code points; a bytes is a sequence of numbers 0-255. encode turns the first into the second and decode goes back, and the two lengths do not have to match - café is four characters and five UTF-8 bytes. Everything else in the objective follows from that.
Strings in Depth Guide 9 of 25 Intermediate
- OSUbuntu 26.04 LTS
- Python3.14.4
- pip25.1.1
- TimeAbout 17 min
Python 3 made str Unicode and bytes a separate type, which is the single largest Python 2 to 3 change. Anything using unicode(...) or str.decode() is Python 2. The default source encoding is UTF-8 and has been since 3.0.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Python 3.14.4 - the only machine this path needs | 2 Core | 4 GB | 50 GB |
This guide includes
Use this for the character-representation objective. This matters because nothing in a byte says which encoding produced it - so the same bytes read two ways give two different answers, and only one of them is your text.
- seeing a string as a sequence of code points
- converting between
strandbytes, and keeping the two types apart - writing the same text in three encodings
- meeting the two ways encoding and decoding fail
- reading the same bytes two ways, and finding what a bytes object really holds
Before you start
-
A string is a sequence of code points
-
str and bytes are different types
-
The same text in three encodings
-
How encoding and decoding fail
-
The same bytes read two ways
-
What bytes actually contain
-
ASCII, and the default
-
What the exam does with this objective