Python Bytes Methods

About This Index
This page is the Python Bytes Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Python 3 script with fixed data, and outputs show bytes exactly as print() displays them.
Learn what a bytes object is on the Bytes hub. Use this page when you need a specific method such as find(), split(), or decode().
Every argument must be a bytes-like object (b"...", bytearray, or memoryview); a str raises TypeError. Case and classification methods only understand ASCII, so bytes 128 to 255 are left alone. removeprefix() and removesuffix() need Python 3.9, and isascii() needs Python 3.7.
How to Call a Bytes Method
Put a dot after any bytes object, then the method name and its arguments. The result is a new object:
packet = b"temp=21;hum=40"
print(packet.find(b";")) # 7
print(packet.split(b";")) # [b'temp=21', b'hum=40']
print(packet.upper()) # b'TEMP=21;HUM=40'
print(packet.decode("ascii")) # temp=21;hum=40
print(packet.hex()[:6]) # 74656d Python Bytes Methods Table
All method guides in this Python tutorial series (42 tutorials), grouped by purpose.
Searching and testing ends
7 methodsCount, find, and locate byte sequences, and test what a bytes object starts or ends with: markers in packets, magic numbers in file headers, and line endings.
| Method | Description | Tutorial |
|---|---|---|
count() | Count non-overlapping occurrences of a byte sequence or a single byte value. | Try it |
find() | Return the lowest index of a byte sequence, or -1 if it is not found. | Try it |
rfind() | Return the highest index of a byte sequence, or -1 if it is not found. | Try it |
index() | Like find(), but raise ValueError when the byte sequence is not found. | Try it |
rindex() | Like rfind(), but raise ValueError when the byte sequence is not found. | Try it |
startswith() | Check whether bytes begin with a prefix or any prefix from a tuple. | Try it |
endswith() | Check whether bytes end with a suffix or any suffix from a tuple. | Try it |
Case, replace, and translate
10 methodsBuild changed copies: ASCII case conversion, replacing byte sequences, byte-by-byte translation tables, tab expansion, and zero padding.
| Method | Description | Tutorial |
|---|---|---|
capitalize() | Return a copy with the first byte uppercased and the rest lowercased (ASCII only). | Try it |
lower() | Return a copy with all ASCII uppercase letters converted to lowercase. | Try it |
upper() | Return a copy with all ASCII lowercase letters converted to uppercase. | Try it |
swapcase() | Return a copy with ASCII uppercase and lowercase letters swapped. | Try it |
title() | Return a copy where each word starts with an uppercase ASCII letter. | Try it |
replace() | Return a copy with occurrences of one byte sequence replaced by another. | Try it |
translate() | Map every byte through a 256-byte table and optionally delete some bytes. | Try it |
maketrans() | Build a 256-byte translation table for translate(). | Try it |
expandtabs() | Replace tab bytes with spaces up to the next tab stop. | Try it |
zfill() | Pad on the left with ASCII zero bytes, keeping a leading sign in front. | Try it |
Strip, trim, and pad
8 methodsRemove unwanted bytes from the ends, cut an exact prefix or suffix, and pad data to a fixed width for records and aligned output.
| Method | Description | Tutorial |
|---|---|---|
strip() | Return a copy with leading and trailing bytes from a set removed (whitespace by default). | Try it |
lstrip() | Return a copy with leading bytes from a set removed (whitespace by default). | Try it |
rstrip() | Return a copy with trailing bytes from a set removed (whitespace by default). | Try it |
removeprefix() | Remove an exact prefix once if present (Python 3.9+). | Try it |
removesuffix() | Remove an exact suffix once if present (Python 3.9+). | Try it |
center() | Center the bytes in a field of a given width using a fill byte. | Try it |
ljust() | Left-justify the bytes in a field, padding on the right with a fill byte. | Try it |
rjust() | Right-justify the bytes in a field, padding on the left with a fill byte. | Try it |
Split and join
6 methodsBreak bytes into fields, lines, or head and tail pieces, and glue a list of bytes objects back together with a separator.
| Method | Description | Tutorial |
|---|---|---|
split() | Split bytes into a list of bytes using a separator or runs of whitespace. | Try it |
rsplit() | Split bytes from the right, so maxsplit keeps the leftmost part together. | Try it |
splitlines() | Split bytes into lines at \n, \r\n, and \r boundaries. | Try it |
partition() | Split at the first separator into a 3-tuple: before, separator, after. | Try it |
rpartition() | Split at the last separator into a 3-tuple: before, separator, after. | Try it |
join() | Concatenate an iterable of bytes-like objects with this bytes as the separator. | Try it |
Classification checks
8 methodsTrue or False tests for ASCII letters, digits, whitespace, case, and title case. Bytes 128 to 255 never count as letters or digits.
| Method | Description | Tutorial |
|---|---|---|
isalnum() | True if all bytes are ASCII letters or digits and there is at least one byte. | Try it |
isalpha() | True if all bytes are ASCII letters and there is at least one byte. | Try it |
isascii() | True if every byte is in the ASCII range 0-127; True for empty bytes. | Try it |
isdigit() | True if all bytes are ASCII digits 0-9 and there is at least one byte. | Try it |
islower() | True if there is at least one ASCII lowercase letter and no ASCII uppercase letters. | Try it |
isspace() | True if all bytes are ASCII whitespace and there is at least one byte. | Try it |
istitle() | True if the bytes are title-cased: each ASCII word starts uppercase and continues lowercase. | Try it |
isupper() | True if there is at least one ASCII uppercase letter and no ASCII lowercase letters. | Try it |
Convert to text and hex
3 methodsTurn bytes into a str with an encoding, show them as a hex string, and parse a hex string back into bytes.
Frequently Asked Questions
Start with capitalize()
Open the first guide in the list and work your way through the bytes methods.
13 people found this page helpful
