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
| Name | Type | Description |
|---|---|---|
| source | int | iterable | buffer | str | Size, byte values, buffer or text; default empty. |
| encoding | str | Required for text input. |
| errors | str | Encoding 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))