Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 149 ยท Data Types

bytes and bytearray

Learn Python's binary types. Encode and decode text, slice and search bytes, change data with bytearray, read hex and use struct to pack numbers.

Everything a computer stores, whether a photo, a web page or a Python file, is ultimately a sequence of bytes: whole numbers from 0 to 255. Python has two types for working with that raw data directly. bytes is an immutable sequence of bytes, and bytearray is its changeable twin. You'll meet them whenever you open a file in binary mode, talk to a network socket, compute a hash or deal with an image format.

The most important idea is the difference between text and bytes. A str holds characters. A bytes object holds numbers. To move between them you need an encoding, a rule that says how each character becomes bytes. In this lesson you'll create bytes, convert them to and from text, slice and search them, change them with bytearray, and pack numbers into binary layouts with struct.

Text versus bytes

A bytes literal looks like a string with a b in front. Each element is an integer, not a one-character string:

Python Creating bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only ASCII characters may appear directly inside a b"..." literal. Anything else must be written as an escape such as \xe9. Notice that indexing gives an int (72 for H), while slicing gives a bytes object, which is different from how strings behave.

flowchart LR
    A["str<br/>'cafรฉ'"] -- "encode('utf-8')" --> B["bytes<br/>UTF-8 data"]
    B -- "decode('utf-8')" --> A

Text becomes bytes with encode() and bytes become text with decode(). Both sides must agree on the encoding.

Encoding and decoding

str.encode() turns text into bytes, and bytes.decode() turns bytes back into text. The default and the right choice almost always is "utf-8":

Python encode and decode

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In UTF-8, plain English letters take one byte, accented letters take two, and symbols like โ˜• take three. That is why a string's len() and its encoded length can differ. Decoding with the wrong encoding either produces nonsense (cafรƒยฉ) or raises UnicodeDecodeError.

When you can't trust the input, decode() accepts an errors argument: "replace" swaps bad bytes for a placeholder and "ignore" drops them.

Tip: Decode bytes to text as soon as they enter your program, work with str inside, and encode again only when you send data out. Mixing the two is the source of most Unicode bugs. Python refuses b"a" + "b" on purpose.

Working with bytes

bytes supports many of the same operations as strings: slicing, in, find(), split(), replace(), startswith() and more. Methods take bytes arguments, not strings:

Python Searching and slicing bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because bytes is immutable, these methods return new objects, and packet[0] = 71 raises TypeError. Comparing, hashing and using bytes as dictionary keys all work, just like with strings.

Hex strings

Binary data is often shown as hexadecimal text, two characters per byte. Python converts both ways:

Python Hex conversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

int.from_bytes() and int.to_bytes() convert between integers and bytes. You choose the byte order: "big" puts the most significant byte first (the network standard), "little" puts it last.

bytearray: bytes you can change

A bytearray has the same methods as bytes plus the ability to modify itself in place. It also has append(), extend(), pop() and item assignment, similar to a list of small integers:

Python Changing a bytearray

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Use a bytearray when you build binary data piece by piece or modify a buffer in place, for example filling a network buffer or flipping individual pixels. Use bytes for data that should not change, such as dictionary keys or the result of hashlib digests.

flowchart TB
    subgraph imm["bytes (immutable)"]
        direction LR
        i0["72"] --- i1["105"]
    end
    subgraph mut["bytearray (mutable)"]
        direction LR
        m0["72"] --- m1["105"] --- m2["33"]
    end
    imm -- "bytearray(b)" --> mut
    mut -- "bytes(b)" --> imm

Convert freely between the two; each conversion makes a copy.

Packing numbers with struct

Many file formats and network protocols define exact binary layouts: "a 2-byte length, then a 4-byte float". The struct module packs Python values into bytes and unpacks them again, using a format string:

Python struct pack and unpack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A < or > at the start of the format fixes byte order and removes padding, which is what you want for portable files. Common codes are b/B (1 byte), h/H (2), i/I (4), q/Q (8), f, d and s for byte strings.

Binary files and memoryview

Open a file with "rb" or "wb" to read and write bytes instead of text, as covered in Files. Reading a file in binary mode gives you a bytes object, with no newline translation and no decoding.

For large buffers, slicing copies data. A memoryview gives you a window onto the same memory without copying:

Python
big = bytearray(1_000_000)
view = memoryview(big)
chunk = view[100:200]     # no copy
chunk[0] = 255            # changes `big` as well

Reach for memoryview only when profiling shows copying is a bottleneck. Everyday code is fine with plain slices.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does b'ABC'[0] return?

    Indexing a bytes object gives an integer, here the byte value 65. Slicing, such as b'ABC'[0:1], would give b'A'.
  2. 2. Which call turns the string s into bytes?

    encode() is a string method that returns bytes. decode() goes the other way and belongs to bytes.
  3. 3. What happens with data = b'abc'; data[0] = 65?

    bytes cannot be changed. Use a bytearray if you need item assignment.
  4. 4. How many bytes does 'รฉ'.encode('utf-8') produce?

    UTF-8 uses one byte for ASCII characters and two bytes for most accented Latin letters such as รฉ.

Summary

  • bytes is an immutable sequence of integers from 0 to 255; bytearray is the mutable version.
  • Indexing returns an int; slicing returns the same type as the original.
  • Convert text and bytes with str.encode() and bytes.decode(), and prefer UTF-8.
  • hex(), bytes.fromhex(), int.to_bytes() and int.from_bytes() convert to and from hex and integers.
  • struct packs and unpacks fixed binary layouts; memoryview avoids copies of large buffers.

Next: Shallow and Deep Copies, where you'll learn when assigning a variable really copies the data and when it doesn't.