Learn Python
>>> variables_data_types.unicode

Unicode & Character Encoding in Python

#concept

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.

#how it works
1

Every character has a code point

A unique number identifying it, viewable with the built-in ord() function.

2

chr() goes the other direction

Turns a code point back into its character.

3

Strings hold any Unicode character natively

Accented letters, CJK characters, and emoji can all sit in the same string as plain ASCII text.

4

encode() converts text to bytes

Using a specified encoding — almost always UTF-8 in modern code.

5

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.

#example
print(ord("A"))
print(chr(9731))
 
text = "café"
encoded = text.encode("utf-8")
print(encoded)
print(encoded.decode("utf-8"))
Output
65
☃
b'caf\xc3\xa9'
café
1ord("A") returns 65, the Unicode code point for the letter A.
2chr(9731) does the reverse, turning code point 9731 into the snowman character.
3text holds a string containing an accented character — Python handles it exactly like any other character, no special declaration needed.
4encode("utf-8") converts the string into a bytes object — the accented character becomes two bytes, since UTF-8 uses multiple bytes for many non-ASCII characters, visible here as two escaped hex sequences.
5decode("utf-8") reverses the process, turning those bytes back into the original, readable string.
#use cases
Reading and writing text files that contain non-English characters, which requires specifying the right encoding
Debugging a UnicodeDecodeError, one of the most common errors when working with files or network data from an unknown source
Handling emoji, accented names, or any non-ASCII text in a web application
Understanding why a string's byte length and character length can differ once encoded

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.