The Bytes Function in Python

The bytes and bytearray are the core built-in types in Python to manipulate binary data.

The bytes function returns bytes object which is an immutable sequence of single bytes.

How to use bytes function

The general syntax for using the bytes function is:

class bytes([source[, encoding[, errors]]])

Where the source can be an integer, string, Object, or iterable.

All the parameters in bytes function are optional. So, if the source is not provided, the bytes function creates an array of zero size.

If a string is used as a source then make sure you provide the encoding argument as well.

The third parameter error is used when the source is provided as a string and it fails.

An example of using an integer in byte function

The example below uses just the first argument in the bytes function i.e. integer as the source. I provided value 9 and see the output:

#Bytes Demo

obj_byte = bytes(2)

print (obj_byte)

The result:


The example of using a string in bytes function

In this examples, I used a string and also given the encoding parameter value i.e. “utf-8”. See the code and output:

#Bytes Demo with string

obj_byte = bytes("Hello Python", "utf-8")

print (obj_byte)

The output:

b’Hello Python’

 Note: The TypeError occurs if you do not provide encoding type there. That is:

TypeError: string argument without an encoding

The example of bytes with a list

In the example below, we have a list of numeric elements. Remember, the definition of bytes function states that you may use an iterator as the source?

The code below uses a list and displays the returned bytes object:

#Bytes Demo with list

lst_num = [1, 15, 25]

obj_byte_lst = bytes(lst_num)

print (obj_byte_lst)

The result:


What if the list contained a string item?

I just added a string item in the above list and see the output:

#Bytes Demo with list (a string item)

lst_mix = [1, 15, 25, "A string"]

obj_byte_lst = bytes(lst_mix)

print (obj_byte_lst)

The result:

TypeError: ‘str’ object cannot be interpreted as an integer

An example of Python bytearray example

As mentioned earlier, the bytes function returns a byte object which is immutable. If you want a mutable object then use the bytearray() function.

The bytearray() function returns a bytearray object that is mutable. The bytearray object is an array of the given bytes.

The syntax for using this function is:

class bytearray([source[, encoding[, errors]]])

The example below shows using an integer as the source in the bytearray function:

#Bytearray Demo

int_x = bytearray(5)

print (int_x)

The result:


Using a string in bytearray example

The example below shows using a string in bytearray function.

#Bytearray Demowith string

obj_bytearray = bytearray("Python is cool", "utf-8")

print (obj_bytearray)

The result:

bytearray(b’Python is cool’)

Author - Atiq Zia

Atiq is the writer at, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!