> ## 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.

# Python bytes

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.

```python theme={"dark"}

# 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.

```python theme={"dark"}

# 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.

```python theme={"dark"}

# 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.

```python theme={"dark"}
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.

```python theme={"dark"}
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'
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
