Bytearray Methods

bytearray.decode()

Decode a mutable byte buffer 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 result is independent immutable text; later changes to the bytearray do not change it.

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 = bytearray("سلام", "utf-8")text = packet.decode()packet[0] = 65print(text)print(bytearray(b"A\xffB").decode(errors="ignore"))print(type(text).__name__)
Python Reference