Bytes Methods

bytes.decode()

Decode an immutable binary sequence as Unicode text using a named text codec. UTF-8 is the default. A strict decoding failure raises UnicodeDecodeError; ignore omits malformed input, replace inserts a replacement character, and surrogateescape can preserve certain undecodable bytes for a compatible round trip. The encoding must match the original data; successful decoding does not establish that the encoding was correct. Unknown codecs raise LookupError. An invalid error-handler name may be detected only on failure or in development/debug mode. The bytes are unchanged; str(data) without an encoding produces a representation rather than decoding.

Formal Syntax & Structure

python syntaxpython
data.decode(encoding='utf-8', errors='strict')

Parameters & Attributes

NameTypeDescription
encodingstrName of a text encoding; UTF-8 by default.
errorsstrRegistered decoding error policy; strict by default.

Return Value

str

Decoded Unicode text; the source is not mutated.

Working Example

Example Snippetpython
packet = b"\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85"print(packet.decode("utf-8"))print(b"A\xffB".decode("utf-8", errors="replace"))print(b"\xe9".decode("latin-1"))
Python Reference