In this article, I will tell you what are bytes in python, what is the syntax of bytes() function in python. It will also tell you how to use the python bytes() function to encoding or decoding data, reading and writing binary files, or working with network protocols that use binary data with examples.
1. What Are Bytes In Python?
- In Python,
bytes
is a built-in data type that represents a sequence of immutable bytes. - It is used to store binary data, such as images, audio, or network packets.
- Bytes are similar to strings, but instead of representing characters, they represent raw binary data.
- A bytes object contains a sequence of integers, each of which represents a byte of data.
- The range of possible values for each byte is 0 to 255 (or 0x00 to 0xFF in hexadecimal notation).
- Bytes can be created using the
bytes()
constructor or by using a byte string literal with ab
prefix. - For example, here is how to create a bytes object using the
bytes()
constructor:>>> bytes_obj = bytes([65, 66, 67]) >>> print(bytes_obj) b'ABC'
- And here is how to create a bytes object using a byte string literal:
>>> byte_str = b'Hello, world!' >>> print(byte_str) b'Hello, world!'
- Note that bytes objects are immutable, which means you cannot modify them once they are created. However, you can create a mutable version of a bytes object using the
bytearray()
constructor ( How To Create, Modify, Append ByteArray Object In Python ).
2. The Python bytes() Function Syntax.
- The
bytes()
function in Python is a built-in function that returns an immutable bytes object. - The general syntax for the
bytes()
function is:bytes([source[, encoding[, errors]]])
source
(optional): an object that can be converted to a sequence of integers (e.g., a list, tuple, or bytes-like object).encoding
(optional): the encoding to use when converting a string to bytes. Default is'utf-8'
.errors
(optional): the error handling scheme to use for encoding errors. Default is'strict'
.- If
source
is not provided, an empty bytes object is returned. - Here are some examples of using the
bytes()
function:# Creating a bytes object from a list of integers bytes_obj = bytes([65, 66, 67]) print(bytes_obj) # b'ABC' # Creating a bytes object from a string str_obj = "hello" bytes_obj = bytes(str_obj, "utf-8") print(bytes_obj) # b'hello' # Creating an empty bytes object empty_bytes = bytes() print(empty_bytes) # b'' # Create a bytearray object bytearray_obj = bytearray([65, 66, 67]) # Create a bytes object from the bytearray bytes_obj = bytes(bytearray_obj) print(bytes_obj) # b'ABC' # Create a bytes object from a hex string byte_hex = bytes.fromhex("48656c6c6f") print(byte_hex) # b'Hello'
3. How To Declare Bytes In Python.
- To declare bytes in Python, you can use the
bytes()
constructor or a byte string literal with ab
prefix. - Using bytes() constructor:
# Create a bytes object from a list of integers bytes_obj = bytes([65, 66, 67]) # Create a bytes object from a string str_obj = "hello" bytes_obj = bytes(str_obj, "utf-8")
- Using a byte string literal:
# Create a byte string from a string byte_str = b"hello" # Create a byte string from a hex string byte_hex = bytes.fromhex("48656c6c6f")
4. Use Python bytes() Function To Encoding Decoding Data Example.
4.1 Encoding Data using Python bytes()
Function.
- To encode a string or any other data into bytes using the
bytes()
function in Python, we can simply pass the data as an argument to thebytes()
function. - Here is an example of encoding a string “hello world” into bytes:
>>> string_data = "hello world" >>> bytes_data = bytes(string_data, encoding='utf-8') >>> print(bytes_data) b'hello world'
- In the above example, we have passed the string “hello world” to the
bytes()
function with the encoding parameter as ‘utf-8’. Thebytes()
function then returns the encoded bytes object, which we have assigned to the variablebytes_data
. Finally, we have printed the value ofbytes_data
, which isb'hello world'
.
4.2 Decoding Data using Python bytes()
Function.
- To decode the bytes data into a string or any other format in Python, we can use the bytes object’s decode() function passing the encoding parameter.
- Here is an example of decoding the bytes data “hello world” into a string:
>>> bytes_data = b'hello world' >>> string_data = bytes_data.decode('utf-8') >>> print(string_data) hello world
- In the above example, we call the bytes object’s decode(‘utf-8’) function to convert the bytes string to plain string.