Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The bytes type in Python is an immutable sequence of integers in the range 0 <= x < 256. It is the fundamental built-in data structure for representing raw binary data, contrasting directly with the str type, which represents sequences of Unicode characters.

Instantiation and Syntax

Byte Literals Prefixing a string literal with b or B creates a bytes object. Only ASCII characters are permitted within byte literals. Bytes with numeric values of 128 or greater must be expressed using hexadecimal, octal, or other escape sequences.

# ASCII literal
b_data = b'hello'


# Hexadecimal escape sequence representing the same data
b_hex = b'\x68\x65\x6c\x6c\x6f'
The bytes() Constructor The constructor instantiates bytes objects from iterables of integers (where 0 <= x < 256), integers, strings (which strictly require an encoding argument), or objects implementing the buffer protocol.

# From an iterable of integers
b_iter = bytes([104, 101, 108, 108, 111])


# Zero-filled bytes object of a specific length
b_zeros = bytes(5)  # b'\x00\x00\x00\x00\x00'


# From a string (encoding is mandatory in the constructor)
b_from_str = bytes("hello", encoding="utf-8")
Encoding Strings Converting a Unicode str to bytes can also be achieved using the string’s encode() method. In Python 3, this method defaults to the UTF-8 encoding scheme and can be called without arguments, though alternative encodings can be explicitly provided.

# String to bytes using default UTF-8 encoding
b_encoded = "Python".encode()


# Bytes back to string
s_decoded = b_encoded.decode('utf-8')

Core Characteristics

Immutability Like str and tuple, bytes objects are immutable. They cannot be modified in place. Any operation that alters the data will allocate and return a new bytes object in memory. Indexing and Slicing Because bytes are fundamentally sequences of integers, indexing a single position evaluates to an int. Conversely, slicing a bytes object evaluates to a new bytes object.
b_data = b'Python'


# Indexing returns an integer (the ASCII/byte value of 'P')
char_val = b_data[0]  # 80


# Slicing returns a bytes object
slice_val = b_data[0:2]  # b'Py'
Method Parity with str The bytes class implements a nearly identical API to the str class, including methods like split(), find(), replace(), and upper(). However, passing a str to these methods will raise a TypeError. The argument requirements depend strictly on the method:
  • Methods like split() can be called without arguments to split on ASCII whitespace. If a delimiter is provided to split() or a substring to replace(), it must be a bytes-like object. Passing an integer or string will raise a TypeError.
  • Methods like find() and count() accept either bytes-like objects or an integer representing a single byte value (0 <= x < 256).
  • Methods like upper() and lower() take no arguments and operate directly on the underlying byte values.
b_text = b'foo bar baz'


# split() without arguments defaults to splitting on whitespace
b_split_default = b_text.split()  # [b'foo', b'bar', b'baz']


# split() with a specific delimiter requires a bytes-like object
b_split_delim = b_text.split(b' ')  # [b'foo', b'bar', b'baz']


# replace() requires bytes-like objects
b_replaced = b_text.replace(b'foo', b'qux')  # b'qux bar baz'


# find() accepts a bytes-like object or an integer
idx = b_text.find(98)  # 4 (index of 'b', which is ASCII 98)


# upper() takes no arguments
b_upper = b_text.upper()  # b'FOO BAR BAZ'
Master Python with Deep Grasping Methodology!Learn More