Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.decode

bytearray — encoding

Decode to a str using an encoding (UTF-8 by default).

Syntax

ba.decode(...)

A UnicodeDecodeError here usually means a bad chunk boundary rather than a wrong encoding: cut a stream at an arbitrary byte offset and a multi-byte character gets sliced in half, even though the data is perfectly valid. Silencing it with errors='ignore' quietly drops characters — read the missing bytes first, or use an incremental decoder from codecs.

Examples

print(bytearray(b'hi').decode())   # → hi
print(bytearray(b'\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82').decode('utf-8'))   # → привет
ba = bytearray(b'name=Ann'); print(ba.decode().split('='))   # → ['name', 'Ann']
print(bytearray(b'ab\xff').decode('utf-8'))   # → UnicodeDecodeError
print(bytearray(b'ab\xff').decode('utf-8', 'ignore'))   # → ab
print(type(bytearray(b'hi').decode()))   # → <class 'str'>

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy