Blog › Python · Published 2026-11-26

How to convert bytes to a string in Python 3

Decode the bytes with the right encoding:

b = b"hello"
s = b.decode("utf-8")
# or
s = str(b, "utf-8")

If the data may contain bad bytes:

b.decode("utf-8", errors="replace")

Do not use str(b) alone. It returns the text b'hello' instead of decoding. To go the other way use s.encode("utf-8").

Official sources

Need help with your project?
We build websites, systems and automation. Start your project WhatsApp

All articles