Skip to main content

bytes

An immutable sequence of bytes.

Bytes values are created with b"..." literals or the bytes() constructor. They support indexing (b[i] returns an int in 0–255), slicing (b[i:j] returns a bytes), membership (x in b), concatenation (+), repetition (*), and len().

len(b"abc") == 3
b"abc"[0] == 97
b"abc"[1:] == b"bc"
97 in b"abc"
b"ab" + b"cd" == b"abcd"
b"ab" * 2 == b"abab"
def bytes(x, /)

bytes: construct a bytes value.

bytes(x) converts its argument to a bytes value.

If x is already a bytes value, the result is x.

If x is a string, its UTF-8 encoding is returned.

If x is an iterable of integers (each in 0–255), the bytes are constructed from those integer values.

bytes(b"hello") == b"hello"
bytes("hello") == b"hello"
bytes([104, 101, 108, 108, 111]) == b"hello"

bytes.elems

def bytes.elems(
) -> typing.Iterable[bytes]

bytes.elems: returns an iterable of the bytes in the byte string, each as a length 1 value of type bytes.

list(b"abc".elems()) == [b"a", b"b", b"c"]