Built-in Functions and Types

bytearray()

Construct a mutable byte sequence using a size, iterable of byte values, buffer or explicitly encoded text. Individual items are integers from 0 to 255. It supports in-place replacement and resizing, except that an active buffer export can prevent resizing. Construction from a mutable buffer copies the data; use memoryview to access the original storage.

Formal Syntax & Structure

python syntaxpython
bytearray(source=b'')bytearray(source, encoding, errors='strict')

Parameters & Attributes

NameTypeDescription
sourceint | iterable | buffer | strSize, byte values, buffer or text; default empty.
encodingstrRequired for text input.
errorsstrEncoding error policy; default strict.

Return Value

bytearray

Mutable sequence of bytes.

Working Example

Example Snippetpython
packet = bytearray(b"ABC")packet[1] = 90packet.extend(b"!")print(packet.decode("ascii"))print(list(packet))
Python Reference