01_Text_and_Bytes — Python's text model¶
Level: 101 → 301 · for Python programmers
Python 3's defining decision is that text and bytes are different types and it will not convert between them behind your back. Everything in this chapter follows from that one choice: why len() gives two answers, why open() is a bet, why a Polish name list sorts wrong, and why a filename is not quite a string.
This is the chapter to read first — not because text is the most important part of Python, but because it is the part where a wrong mental model survives longest before failing, and it fails in production on data from someone else's country.
| # | Lesson | The question it answers | Status |
|---|---|---|---|
| 1 | str is not bytes |
Which type am I holding, and why won't Python mix them? | written |
| 2 | String literals | What do the r, b and f prefixes change — and why is '\d' two characters? |
written |
| 3 | Encode and decode | Which direction is which, and what does errors= throw away? |
written |
| 4 | Making a bytes object |
Why is bytes(5) five zero bytes and bytes([5]) one? |
written |
| 5 | bytearray is the mutable one |
Which of the two binary types can I write into, and what does mutability cost? | written |
| 6 | Counting characters | How long is this string — and which of the four answers did you want? | written |
| 7 | Slicing is not indexing | Why does s[100] raise when s[:100] does not — and what does s[::-1] do to an accent? |
written |
| 8 | Is it a letter? | What do the twelve is* predicates actually test? |
written |
| 9 | Lowercasing is not folding | Why do 'straße' and 'STRASSE' still differ after .lower() — and what is casefold() for? |
written |
| 10 | repr is not str |
Why is a space "printable" and a tab not — and what did str(b'x') just do? |
written |
| 11 | What ends a line | Which characters count as a line break — and why do I get three different answers? | written |
| 12 | strip is a set, not a prefix |
Why did lstrip('Arthur: ') eat three more characters than I asked for? |
written |
| 13 | Four ways to find it | Why did a search that found nothing hand back the last character of my string? | written |
| 14 | translate is a table, keyed by ordinal |
Why is maketrans a separate call, and why is translate one pass where three .replace() calls are not? |
written |
| 15 | The format mini-language | Is f'{x:>8}' the same grammar as '{:>8}'.format(x) — and where does % fit? |
written |
| 16 | Padding is not alignment | Why is my column still ragged after ljust(20) — and why did zfill keep the minus sign? |
written |
| 17 | Comparison has a mode | Which of the four ways to compare two strings did I just use? | written |
| 18 | Normalization | Why can 'é' == 'é' be False? |
stub |
| 19 | Sorting is not comparing | Why does sorted() put Łukasiewicz after Zawadzki? |
written |
| 20 | Opening a file | What does open() decide for me besides the encoding? |
written |
| 21 | Standard in, standard out, and pipes | Why does piping my program into head raise BrokenPipeError — and how do I write bytes to stdout? |
stub |
| 22 | Filenames are not text | How does Python read a filename that isn't valid UTF-8? | written |
| 23 | What kind of file is this? | Which of the five APIs answers the question I actually asked? | written |
| 24 | The string module |
What is left in a module named string, and why is capwords not title? |
written |
| 25 | The codecs registry | What is in the registry that .encode() and .decode() cannot reach — and why does decoding a stream in chunks differ? |
written |
| 26 | bin() is not the bits |
Why is bin(-9) '-0b1001' — and where did the bits go? |
written |
Several of these pages end with a ## Practice section — a kata: predict the answer, then run it, then check. They are indexed in KATAS.md, which is also the only place they are put in an order, and that order is not this one: it puts the four bytes katas first, because that is the thing this chapter's readers report bouncing off.
Where this sits relative to the other libraries¶
The encodings library ↗ teaches encodings as a subject, using four languages to illustrate. This chapter teaches Python's answer to that subject — the type boundary, the codec registry, the errors= policies, the locale coupling. Where a page here needs the language-agnostic groundwork, it links there rather than repeating it. The crosswalk is the index of which idea lives where.