Skip to content

bytes

The built-in bytes data type allows you to represent and manipulate immutable sequences of bytes, which are numbers in the range 0 <= x < 256. This data type is particularly useful for handling binary data, encoding and decoding text, file input and output, and network communication:

Language: Python
>>> b'Hello, World!'
b'Hello, World!'

>>> b'\x48\x65\x6c\x6c\x6f'
b'Hello'

bytes Constructors

Language: Python Syntax
bytes(source=b"")
bytes(source, encoding)
bytes(source, encoding, errors)

Arguments

Argument Description Default Value
source A bytes literal, a string, an integer that sets the length of a zero-filled bytes object, or an iterable of integers b""
encoding The character encoding to use for encoding source if it holds a string Required argument
errors A handler for encoding and decoding errors "strict"

Return Value

  • Returns a Python bytes object

bytes Examples

Creating an empty bytes object:

Language: Python
>>> bytes()
b''

Create bytes objects using literals:

Language: Python
>>> b'This is a bytes literal with single quotes'
b'This is a bytes literal with single quotes'

>>> b"Another bytes literal, now with double quotes"
b'Another bytes literal, now with double quotes'

Using the class constructor with a string requires specifying an encoding:

Language: Python
>>> bytes("Hello, World!", encoding="utf-8")
b'Hello, World!'

Creating a bytes object from an iterable of integers:

Language: Python
>>> bytes([72, 101, 108, 108, 111])
b'Hello'

bytes Methods

Method Description
.count() Returns the number of non-overlapping occurrences of a byte or sub-sequence
.decode() Decodes the bytes using a specified encoding
.endswith() Returns True if the byte sequence ends with a specified suffix
.find() Returns the lowest index in the bytes where the sub-sequence is found
.index() Like find(), but raises a ValueError if the sub-sequence is not found
.join() Concatenates a sequence of bytes or bytearrays with the bytes object as separator
.replace() Returns a copy of the bytes with all occurrences of a sub-sequence replaced
.split() Splits the bytes into a list using a specified separator
.startswith() Returns True if the byte sequence starts with a specified prefix
.strip() Returns a copy of the bytes with leading and trailing whitespace removed

bytes Common Use Cases

The most common use cases for the bytes data type include:

  • Handling binary data in files or network communication
  • Encoding and decoding text
  • Working with protocols that require binary data representation

bytes Real-World Example

Say that you want to read a binary file and extract specific data. You can use the bytes data type to efficiently handle this task:

Language: Python
>>> with open('example.bin', 'rb') as file:
...     data = file.read()
...     print(data[:10])
...
b'\x89PNG\r\n\x1a\n\x00\x00'

In this example, you open a binary file and read its contents into a bytes object. You then print the first 10 bytes to inspect the file’s header. The bytes data type allows you to easily manage and manipulate this binary data.

Bytes Objects: Handling Binary Data in Python

Tutorial

Bytes Objects: Handling Binary Data in Python

In this tutorial, you'll learn about Python's bytes objects, which help you process low-level binary data. You'll explore how to create and manipulate byte sequences in Python and how to convert between bytes and strings. Additionally, you'll practice this knowledge by coding a few fun examples.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated July 22, 2026 • Reviewed by Brenda Weleschuk and Dan Bader
Morty Proxy This is a proxified and sanitized view of the page, visit original site.