Python bytes.join() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

sep.join(iterable) returns a new bytes object made by concatenating every item of iterable, with sep inserted between neighbors. The separator is the object you call the method on, for example b",".join(fields).

Every item must be a bytes-like object: bytes, bytearray, or memoryview. Using b"".join(parts) is the efficient way to assemble many pieces, much faster than repeated + in a loop.

bytes.join() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.join(...). No import is needed, and bytearray objects have the same method.

Syntax

Call it on the separator and pass the pieces.

python
result = sep.join(iterable)
result = b"".join(parts)
CallWhat it does
b",".join([b"a", b"b"])Returns b'a,b'.
b"".join([b"\x01", b"\x02"])Returns b'\x01\x02'.
b"-".join([])Returns b''.

The separator only goes between items, never before the first or after the last.

Method signatures
python
bytes.join(iterable, /)
# iterable: items must be bytes-like objects

Parameters

ParameterDescription
iterableAny iterable (list, tuple, generator) whose items are bytes-like objects.

Return Value

Return ValueDescription
bytesThe items concatenated with the separator between them (bytearray when the separator is a bytearray).

Python Examples

Example 1 — Build a CSV Row and CRLF Protocol Lines

Join fields with commas, protocol lines with CRLF, and raw bytes with no separator at all.

python
fields = [b"7", b"ada", b"98"]

print(b",".join(fields))
print(b"\r\n".join([b"HELLO", b"WORLD"]) + b"\r\n")
print(b"".join([b"\x01", b"\x02", b"\x03"]))

How It Works

  1. Separator between. Two commas join three fields.
  2. Terminate the last line. join() adds CRLF only between lines, so the final one is appended separately.
  3. Empty separator. b"" simply concatenates the pieces.

Example 2 — Every Item Must Be bytes-like

A str item breaks the join. Encode numbers first, and see that bytearray and memoryview items are fine.

python
parts = [b"a", "b", b"c"]

try:
    b"-".join(parts)
except TypeError:
    print("every item must be bytes-like")

numbers = [1, 2, 3]
print(b"-".join(str(n).encode("ascii") for n in numbers))
print(b"".join([bytearray(b"x"), memoryview(b"y")]))

How It Works

  1. One bad item. The str "b" in the middle raises TypeError.
  2. Encode numbers. A generator converts each int to its ASCII digits before joining.
  3. Mixed bytes-like items. bytearray and memoryview items are accepted, and the result is bytes because the separator is bytes.

Example 3 — Assemble a Binary Packet

A packet has a two-byte sync header, a one-byte length, and the payload. Join the pieces, then inspect the result.

python
payload = b"PING"
packet = b"".join([b"\xaa\x55", bytes([len(payload)]), payload])

print(packet)
print(packet.hex(" "))
print(len(packet))

How It Works

  1. Length byte. bytes([len(payload)]) turns the int 4 into the single byte \x04.
  2. Surprising repr. 0x55 is the ASCII code for U, so the repr shows U instead of \x55.
  3. Hex view. hex() with a separator shows all seven bytes clearly.

Frequently Asked Questions

Call join on the separator: b",".join(items), or b"".join(items) for no separator.
The items are str. Every item must be bytes-like, so use [b"a", b"b"] or encode the strings.
For raw byte values use bytes(list_of_ints). For ASCII digits use b",".join(str(n).encode("ascii") for n in nums).
Yes. join() computes the final size once, while repeated + creates a new object each time.
Empty bytes, b"".

Next: bytes.ljust()

Left-justify the bytes in a field, padding on the right with a fill byte.

ljust() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful