Python bytearray() Function with Examples

Python offers a wide range of built-in functions that make programming easier and more efficient. One such function is the bytearray() function in Python.

The bytearray() function is a built-in function that returns a new bytearray object, which is an array of the specified bytes.

This function is similar to the bytes() function, but it is mutable, which means that we can modify it after it is created.

The main purpose of the bytearray() function is to create a mutable array of byte object in Python that can be modified after creation. It is useful in scenarios where we need to manipulate binary data.

Syntax of bytearray() Function in Python

The general syntax of the bytearray() function in Python is as:

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

In the above syntax, the parameters of bytearray() function are optional. If we do not specify any parameters, the function returns an empty bytearray object.

The first parameter “source” can be a string, a bytes object, an iterable of integers, or an integer that defines the size of the bytearray object to create.


The bytearray() function takes three parameters that are as:

source (optional): This is the first parameter that specifies the source of data to create the bytearray object.

  • If the source parameter is a string, we must encode it with the specified encoding.
  • If the source parameter is a bytes object, Python copy it into the new bytearray object.
  • If the parameter source is an iterable of integers, it is used to initialize the bytearray object.
  • If the parameter source is an integer, it defines the size of the bytearray object to create.

encoding (optional): This is the second parameter that specifies the encoding of the source if it is a string. The default value is ‘utf-8’.

errors (optional): This is the third parameter that specifies an action to take if the encoding conversion fails.

Return value

The bytearray() function returns a new bytearray object (i.e. an array of bytes). The object’s type is bytearray.

Python bytearray() Function Examples

Let’s take some important example programs based on the bytearray() function provided by Python.

Example 1: Creating an empty bytearray

empty_bytearray = bytearray()

Example 2: Converting a bytearray object to a string

# Python program to convert a bytearray object to a string.
# Creating a bytearray object.
my_bytearray = bytearray(b'Scien, tech, easy!')

# Calling decode() method to convert bytearray object to a string.
my_string = my_bytearray.decode('utf-8')

# Displaying the result.
      Scien, tech, easy!

In this example, we have created an array of byte object and stored it into a variable “my_bytearray”. We then uses the decode() method to convert the bytearray object to a string. We have printed the resulting string on the console.

If the parameter source specified in the bytearray() function is a string and we do not specify encoding, Python returns a TypeError exception. For example:

Example 3:

print(bytearray('Scientech Easy'))
      TypeError: string argument without an encoding

Example 4: Converting a list of integers into bytearray

# Python program to convert an iterable object into bytearray.
# Creating a list of integers.
byte_list = [65, 66, 67, 68, 69] # iterable object

# Calling bytearray() function and passing byte_list.
byte_array = bytearray(byte_list)

# Displaying the result.

In this example, we have created an iterable object (i.e. a list of integers) and stored it into a variable named byte_list. Then, we have used bytearray() function and passed the values of byte_list as an argument to the function. We have stored the resulted bytearray object into a variable named byte_array and printed on the console.

Example 5: Converting integer into bytearray.

# Python program to convert an integer to a bytearray.
n1 = 1
n2 = 2
n3 = 3
result1 = bytearray(n1)
result2 = bytearray(n2)
result3 = bytearray(n3)

print(result1) # A byte array of size 1
print(result2) # A byte array of size 2
print(result3) # A byte array of size 3

How to Modifying bytearray() Function in Python

Bytearray() objects are mutable, meaning that we can easily modify them after we create them. We can modify a bytearray() object using slice notation. Here is an example program:

Example 6:

# Python program to modify a bytearray object.
my_bytearray = bytearray(b'Scientech, Easy')

# Modifying a bytearray object using slice notation.
my_bytearray[5] = 84
      bytearray(b'ScienTech, Easy')

In this example, we have created a bytearray object named “my_bytearray”. We then have modified the fifth element of the bytearray using slice notation. The resulting bytearray object is printed on the console.

Applications of bytearray() in Python

Python Bytearray() function is widely used in various applications, such as:

  • Manipulating binary data
  • Encryption and decryption of data
  • Compression and decompression of data
  • Parsing of binary file formats

Look at the below example where we have used bytearray() function to manipulate binary data.

Example 7:

import struct
my_bytearray = bytearray(b'\x01\x02\x03\x04')

value = struct.unpack('<i', my_bytearray)[0]

In this example program, we have created a bytearray object called “my_bytearray”. We then imported the struct module to unpack the bytearray object into a signed integer value. We have printed the resulted value on the console.

In this example, we have discussed bytearray() function in Python with various examples. Hope that you will have understood the basic concepts of using bytearray() function in the applications.
Thanks for reading!!!

Please share your love