How to Encode and Decode UTF-8 in Python
Convert a Python string to UTF-8 bytes with .encode() and back to text with .decode(), with a simple demo function.
Python code
10 linesdef encode_decode_demo(text: str):
encoded = text.encode("utf-8")
decoded = encoded.decode("utf-8")
print(f"Original string: {text}")
print(f"Encoded bytes: {encoded}")
print(f"Decoded string: {decoded}")
print(f"Match: {text == decoded}")
if __name__ == "__main__":
encode_decode_demo("Hello, 世界! 🌍")
Output
Original string: Hello, 世界! 🌍
Encoded bytes: b'Hello, \xe4\xb8\x96\xe7\x95\x8c! \xf0\x9f\x8c\x8d'
Decoded string: Hello, 世界! 🌍
Match: True
How it works
str.encode('utf-8') converts each character into a sequence of bytes according to the UTF-8 encoding, producing a bytes object. bytes.decode('utf-8') reverses the process, reconstructing the original string. The round-trip is lossless because UTF-8 can represent every Unicode code point. The print statements confirm the decoded string matches the original via the == comparison. This works for ASCII, CJK, and emoji alike.
Common mistakes
- Calling `decode()` on a string instead of a bytes object, causing an AttributeError
- Omitting the encoding argument and relying on the platform default, which may not be UTF-8
- Trying to decode bytes that were encoded with a different encoding, producing UnicodeDecodeError
Variations
- Use `bytes(text, 'utf-8')` as an alternative to `text.encode('utf-8')`
- Use `codecs.encode(text, 'utf-8')` for compatibility with older code
Real-world use cases
- Writing user-generated text to a binary file or socket, ensuring consistent byte encoding.
- Storing Python strings in a database that accepts BLOB or binary columns.
- Hashing or signing text data by first converting it to its UTF-8 byte representation.
Sponsored
More from Strings & text
- Automatically Detect Weak Passwords from Large Password Lists in Python easy
- Build CSV row from Python list with proper quoting easy
- Build a Secure Password Strength Checker in Python easy
- Convert Natural Language Dates to Datetime in Python medium
- Count Characters, Words, and Lines in Python Text easy
- Extract Data from Strings in Python: Beginner's Guide easy
Keep learning
Related tutorials and quizzes for this topic.