Unicode & Character Encoding in Python
Every character Python works with — letters, digits, emoji, characters from any language — is represented internally using Unicode, a standard that assigns a unique number, called a code point, to essentially every character in every writing system. Python 3 strings are Unicode by default, which is why they can hold text in any language mixed freely, with no special setup.
Encoding is the process of converting that text into raw bytes, for saving to a file or sending over a network. Decoding reverses it, turning bytes back into readable text. Both need to agree on which encoding scheme was used — UTF-8 is the near-universal default today.
Every character has a code point
A unique number identifying it, viewable with the built-in ord() function.
chr() goes the other direction
Turns a code point back into its character.
Strings hold any Unicode character natively
Accented letters, CJK characters, and emoji can all sit in the same string as plain ASCII text.
encode() converts text to bytes
Using a specified encoding — almost always UTF-8 in modern code.
decode() converts bytes back to text
Using the same encoding the bytes were created with, or the result comes out garbled or raises an error.
print(ord("A"))print(chr(9731)) text = "café"encoded = text.encode("utf-8")print(encoded)print(encoded.decode("utf-8"))Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.