bytearray()
bytearray([source[, encoding[, errors]]]) Returns:
bytearray object · Updated March 13, 2026 · Built-in Functions built-in bytes mutable binary encoding
The bytearray type is a mutable sequence of bytes. Unlike immutable bytes objects, you can modify bytearray objects in place — useful for building or processing binary data incrementally.
Syntax
bytearray()
bytearray(source)
bytearray(source, encoding)
bytearray(source, encoding, errors)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
source | iterable, int, or str | — | Initial bytes or string to convert |
encoding | str | — | Encoding (required if source is a string) |
errors | str | — | Error handling scheme (see below) |
Encoding Error Handling
| Value | Behavior |
|---|---|
'strict' | Raise UnicodeDecodeError on failure (default) |
'ignore' | Ignore unencodable characters |
'replace' | Replace with ? |
'backslashreplace' | Use Python escape sequences |
Examples
Creating an empty bytearray
data = bytearray()
print(data)
# bytearray(b'')
Creating with initial size
# Useful for buffering
buffer = bytearray(1024) # 1KB zero-initialized
print(len(buffer))
# 1024
From a string with encoding
text = "Hello"
data = bytearray(text, 'utf-8')
print(data)
# bytearray(b'Hello')
From bytes or bytearray
original = b"binary data"
copy = bytearray(original)
print(copy)
# bytearray(b'binary data')
# Modify the copy
copy[0] = 66 # Change 'b' to 'B'
print(copy)
# bytearray(b'Binary data')
From a list of integers
# Each integer must be 0-255
data = bytearray([72, 101, 108, 108, 111])
print(data)
# bytearray(b'Hello')
Modifying in place
data = bytearray(b"hello")
# Change a single byte
data[0] = 72 # 'H'
print(data)
# bytearray(b'Hello')
# Append bytes
data.extend(b' world')
print(data)
# bytearray(b'Hello world')
# Append integer (single byte)
data.append(33) # '!'
print(data)
# bytearray(b'Hello world!')
Common Patterns
Building HTTP response
response = bytearray(b"HTTP/1.1 200 OK\r\n")
# Add headers
response.extend(b"Content-Type: text/html\r\n")
response.extend(b"Content-Length: 13\r\n")
response.extend(b"\r\n")
# Add body
response.extend(b"Hello, World!")
print(bytes(response))
# b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n...'
Reading fixed-size binary records
import struct
# Simulate binary data
record_size = 12
raw_data = bytearray(b"John\x00\x00\x00\x00\x00\x00\x00\x00\x0025")
# Parse: 8-char name + 4-byte age
name = raw_data[:8].rstrip(b'\x00').decode('utf-8')
age = struct.unpack('>I', raw_data[8:12])[0]
print(f"{name}, {age} years old")
# John, 25 years old
Network packet construction
# Build a simple packet: header (4 bytes) + payload
packet = bytearray()
# Header: version (1) + type (1) + length (2)
packet.append(1) # version
packet.append(5) # type
packet.extend(struct.pack('>H', 10)) # payload length
# Payload
packet.extend(b"Hello12345")
print(bytes(packet))
# b'\x01\x05\x00\nHello12345'
Mutability Notes
Unlike bytes, bytearray supports item assignment and in-place operations:
data = bytearray(b"test")
# This works with bytearray
data[0] = 84 # Change 't' to 'T'
print(data)
# bytearray(b'Test')
# This would fail with bytes:
# TypeError: 'bytes' object does not support item assignment