all Python 3Byte sequencesfunction
bytes.join
bytes — split and join
Join a sequence of bytes with this separator.
Syntax
b.join(...)
Every item must be bytes-like — a list of ints or of str raises TypeError. That is the usual trap: iterating over bytes yields ints, so b'-'.join(b'abc') fails instead of producing b'a-b-c'. The separator decides the result type (bytes.join returns bytes even for bytearray items), and joining once is far cheaper than repeated += in a loop.
Examples
print(b'-'.join([b'a', b'b', b'c'])) # → b'a-b-c'
print(b''.join([b'ab', b'cd'])) # → b'abcd'
print(b'+'.join(b'a-b-c'.split(b'-'))) # → b'a+b+c'
print(b'-'.join([bytearray(b'x'), b'y'])) # → b'x-y'
print(b', '.join([])) # → b''
print(b'-'.join([b'a', 'b'])) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy