Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

bytearray()

Constructs a mutable sequence of byte values.

Version: Python 3.14; repository image, production runtime not attested

Syntax

bytearray()
bytearray(source)
bytearray(string, encoding, errors="strict")

Constructs a mutable sequence of byte values.

Assigning an integer changes a single byte in place. Constructing from an integer length yields zero-filled storage.

A bytearray stores mutable byte values. Assigning decimal 65 to position zero replaces the initial ASCII a with A, and bytes(data) captures Abc. An integer constructor argument instead specifies a zero-filled length. Assigning a byte outside 0–255 raises ValueError.

data = bytearray(b"abc")
alias = data
data[0] = 65
assert bytes(alias) == b"Abc" and bytearray(3).hex() == "000000"

Examples

data = bytearray(b"abc")
data[0] = 65
print(bytes(data))
print(bytearray(3).hex())

Result

b'Abc'
000000

A bytearray stores mutable byte values. Assigning decimal 65 to position zero replaces the initial ASCII a with A, and bytes(data) captures Abc. An integer constructor argument instead specifies a zero-filled length. Assigning a byte outside 0–255 raises ValueError.

Sources