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.

Easy Python 3.9+ Aug 9, 2026 Strings & text 14 views 0 copies

Python code

10 lines
Python 3.9+
def 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

stdout
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

  1. Use `bytes(text, 'utf-8')` as an alternative to `text.encode('utf-8')`
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Strings & text

Related tutorials and quizzes for this topic.