3 ms·
Maybe the following helps as a mnemonic: Encoding is always “transform into the cryptic format” and decoding is always “transform from the cryptic format into s
by layer8 4y ago
Maybe the following helps as a mnemonic: Encoding is always “transform into the cryptic format” and decoding is always “transform from the cryptic format into something I understand”. This should be natural, like “decoding a cryptic message”. Now, bytes are the cryptic thing, a binary representation (the “code”), whereas strings are the noncryptic thing (just text). Therefore you encode to bytes, and decode them to a string.
It’s also analogous to encryption/decryption, and to compression/decompression. Bytes are the opaque external representation, the on-disk format. Strings are the intelligible in-program format. You decode the former into the latter.
(Caveat: I don’t know any Python, but the topic of character encoding is independent from the programming language.)