UTF-8
UTF-8 is a variable-width character encoding that stores each Unicode code point in one to four bytes. The name is short for Unicode Transformation Format, where the 8 refers to its 8-bit code units. Ken Thompson and Rob Pike designed the encoding in 1992 for the Plan 9 operating system, and RFC 3629 later standardized it.
Code points up to 127 occupy a single byte with the same value as in ASCII, so every ASCII file is already valid UTF-8. Higher code points take two, three, or four bytes, and the leading byte’s bit pattern announces how many continuation bytes follow.
That design lets a decoder find the start of the next character after corrupted input, and since UTF-8 works byte by byte, it avoids the byte-order ambiguity of UTF-16 and UTF-32. Each code point’s range alone decides how many bytes it needs:
UTF-8 dominates modern computing. It encodes the overwhelming majority of web pages and is the required text encoding for JSON exchanged between systems. In Python, source files are UTF-8 by default, and str.encode() and bytes.decode() assume it when no other encoding is named.
Under a variable-width encoding, a string’s length in characters and its size in bytes are different numbers. Say a program needs to know how many bytes a message will occupy on disk. Encoding individual characters shows that each one’s width depends on its code point:
>>> "A".encode("utf-8")
b'A'
>>> "é".encode("utf-8")
b'\xc3\xa9'
>>> "🐍".encode("utf-8")
b'\xf0\x9f\x90\x8d'
>>> len("café"), len("café".encode("utf-8"))
(4, 5)
Calling .decode("utf-8") on the bytes restores the original string. Decoding them with a different encoding instead raises an error or produces garbled text, commonly called mojibake.
Related Resources
Tutorial
Unicode & Character Encodings in Python: A Painless Guide
In this tutorial, you'll get a Python-centric introduction to character encodings and unicode. Handling character encodings and numbering systems can at times seem painful and complicated, but this guide is here to help with easy-to-follow Python examples.
For additional information on related topics, take a look at the following resources:
- Unicode in Python: Working With Character Encodings (Course)
- How to Convert Bytes to Strings in Python (Tutorial)
- Strings and Character Data in Python (Tutorial)
- How to Sort Unicode Strings Alphabetically in Python (Tutorial)
- How to Convert Bytes to Strings in Python (Quiz)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)
By Martin Breuss • Updated July 12, 2026