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:
>>> b'Hello, World!'
b'Hello, World!'
>>> b'\x48\x65\x6c\x6c\x6f'
b'Hello'
bytes
Constructors
bytes(source=b"")
bytes(source, encoding)
bytes(source, encoding, errors)
Arguments
Argument | Description | Default Value |
---|---|---|
source |
A bytes literal, a string, or an iterable of integers |
b"" |
encoding |
The character encoding to use for decoding 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:
>>> bytes()
b''
Create bytes
objects using literals:
>>> 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:
>>> bytes("Hello, World!", encoding="utf-8")
b'Hello, World!'
Creating a bytes
object from an iterable of integers:
>>> 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:
>>> with open('example.bin', 'rb') as file:
... data = file.read()
... print(data[:10])
...
b'\x89PNG\r\n\x1a\n\x00\x00'
In this example, we open a binary file and read its contents into a bytes
object. We then print the first 10 bytes to inspect the file’s header. The bytes
data type allows us to easily manage and manipulate this binary data.
Related Resources
Tutorial
Basic Data Types in Python: A Quick Exploration
In this tutorial, you'll learn about the basic data types that are built into Python, including numbers, strings, bytes, and Booleans.
For additional information on related topics, take a look at the following resources:
- Python's Built-in Functions: A Complete Exploration (Tutorial)
- Unicode & Character Encodings in Python: A Painless Guide (Tutorial)
- Strings and Character Data in Python (Tutorial)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Python's Built-in Functions: A Complete Exploration (Quiz)
- Unicode in Python: Working With Character Encodings (Course)
- Strings and Character Data in Python (Course)
- Python Strings and Character Data (Quiz)