In Python, bytes is an immutable sequence of integers from 0 to 255. It stores raw binary data: the contents of an image file, a network packet, a checksum, or text that has already been encoded with a codec such as UTF-8.
You write a bytes literal with a b prefix: b"GET" or b'\x89PNG'. Only ASCII characters may appear in the literal itself; any other value is written as an escape such as \xff. You can also build bytes with the bytes() built-in, and you get bytes back from "text".encode("utf-8") or from reading a file opened in "rb" mode.
This page explains what bytes are and the rules every bytes method follows. The Bytes Methods index lists every method guide (find(), split(), decode(), and more).
Indexing gives an int, slicing gives bytes.
b"Hi"[0] is the number 72, but b"Hi"[0:1] is b'H'. This surprises most beginners coming from str, where both give a one-character string.
Syntax
Python Syntax
Create bytes, read values, and call methods with dot notation:
Python
data = b"Hello" # bytes literal
size = len(data) # 5: number of bytes
first = data[0] # 72: indexing gives an int
part = data[1:3] # b'el': slicing gives bytes
loud = data.upper() # b'HELLO': a new object
text = data.decode("ascii") # 'Hello': back to str
print(size, first, part, loud, text)
Reference
bytes vs str vs bytearray
Python keeps text and binary data in separate types. Pick the one that matches your data:
Type
Holds
Remember
str
Unicode text, for example "caf\u00e9"
Immutable; .encode("utf-8") turns it into bytes
bytes
Raw values 0 to 255, for example b"caf\xc3\xa9"
Immutable and hashable; .decode("utf-8") turns it into text
Zero-copy slicing of large buffers; accepted wherever bytes-like is allowed
Common uses for bytes: reading and writing binary files, checking file signatures (magic numbers) such as b"\x89PNG", sending and receiving data over sockets, hashing with hashlib, and building or parsing binary protocols.
Reference
Rules Every Bytes Method Follows
Rule
What it means
New object every time
bytes is immutable, so methods like replace() and strip() return a new bytes object; keep the result in a variable.
Bytes-like arguments only
Pass b"x", a bytearray, or a memoryview. A str argument such as data.find("x") raises TypeError.
ASCII-only case and checks
upper(), isalpha(), isspace(), and friends only know ASCII letters, digits, and whitespace. Bytes 128 to 255 are never changed and never count as letters.
Same methods on bytearray
Called on a bytearray, methods that build data return a bytearray instead of bytes.
Text needs decode()
To use full Unicode rules, call decode() first, work with the str, then encode again.
Code
Python Examples
Three short programs that show how bytes behave in everyday Python.
Example 1 — Indexing, Slicing, and Looping Over bytes
See the three ways to look inside a bytes object: as a whole, one value at a time, and as smaller bytes slices.
A bytes object is an immutable sequence of integers from 0 to 255. It holds raw binary data such as file contents, network packets, or text after it has been encoded. You write one with a b prefix, for example b"Hi".
str holds Unicode text made of characters; bytes holds raw byte values. Convert text to bytes with text.encode("utf-8") and bytes back to text with data.decode("utf-8"). The two types never mix: b"a" + "b" raises TypeError.
Indexing a bytes object returns the byte value as an int, so b"A"[0] is 65. Slicing returns bytes, so b"A"[0:1] is b'A'.
No. bytes is immutable, so every method returns a new object and item assignment raises TypeError. Use bytearray when you need to modify data in place.
Bytes case methods only know ASCII letters a-z and A-Z. Bytes from 128 to 255 are not letters to them, so they pass through unchanged. Decode to str first if you need full Unicode case rules.