prize18-3 Exercise 3: Why "No Error" Proves Nothing ==================================================== Prediction: plain English decodes as everything. UTF-8 text with an accent is valid UTF-8 but also valid in every single-byte table. Latin-1 text with an accent is usually not valid UTF-8 (a lone 0xE9 is a lead byte with no continuation bytes). A single-byte table can never fail on bytes it defines, and Latin-1 defines all 256. ex3.py: def which_work(raw, names): good = [] for name in names: try: raw.decode(name) good.append(name) except UnicodeDecodeError: pass return good names = ["ascii", "utf-8", "latin-1", "cp1252", "cp437"] samples = { "plain English": b"hello", "utf-8 text": "caf\u00e9".encode("utf-8"), "latin-1 text": "caf\u00e9".encode("latin-1"), "euro in cp1252": "5\u20ac".encode("cp1252"), } for label, raw in samples.items(): print(f"{label:15} {raw!r:22} decodes as: {which_work(raw, names)}") Real output (Python 3.14): plain English b'hello' decodes as: ['ascii', 'utf-8', 'latin-1', 'cp1252', 'cp437'] utf-8 text b'caf\xc3\xa9' decodes as: ['utf-8', 'latin-1', 'cp1252', 'cp437'] latin-1 text b'caf\xe9' decodes as: ['latin-1', 'cp1252', 'cp437'] euro in cp1252 b'5\x80' decodes as: ['latin-1', 'cp1252', 'cp437'] What it tells you: a successful decode only rules encodings out. Latin-1 (and here cp437) succeed on everything, so they will happily produce wrong text. UTF-8 is the exception: random non-UTF-8 bytes usually fail its strict rules, so "valid UTF-8" is real evidence (though not proof) that the text is UTF-8. The row for the euro shows the opposite danger: the bytes decode "fine" as Latin-1 into a control character instead of a euro sign.