Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python bytearray() function

Python Bytearray() Function

In this article, we will be having a look at one of the Python’s in-built function — Python bytearray() function.

Understanding Python bytearray() function

Python has in-built bytearray() method that creates an array of bytes and returns a bytearray object of the created array of the defined size by a particular iterable or a value.


bytearray(source_input, encoding_scheme, error)


  • source_input: It is an optional parameter. It is basically used to initialize the array data elements. The source_input can be an iterable, value, etc.
  • encoding_scheme(optional): It is used to define the encoding pattern for a string.
  • error(optional): It defines the actions that need to be taken in case the encoding fails.

The bytearray() method takes an iterable such as list, string, tuple, etc or value as an argument and initializes an array with the size and returns a bytearray object of it.

Example: Python bytearray() function with no arguments

inp = bytearray()

When no argument is passed to the bytearray() function, the function returns an empty bytearray object.



1. Python bytearray() function with string as an argument

When a string value is passed as an argument to the bytearray() function, it converts the string into an array of bytes.

The mandatory condition continues to stay such that whenever a string is passed in the parameter list, encoding has to be defined for the same in the parameter list, otherwise a TypeError exception is raised.

Example 1:

inp_str = "JournalDev"

arr_8 = bytearray(inp_str, 'utf-8') 


Here, we have passed the encoding_scheme as ‘utf-8‘ for the input string to be converted to an array of bytes.



Example 2:

inp_str = "JournalDev"

arr_16 = bytearray(inp_str, 'utf-16') 


In the above example, the encoding scheme is defined as ‘utf-16‘.



2. Python bytearray() function with iterable passed as a parameter

When an iterable such as list, set, tuple, etc is passed as an argument to the bytearray() function, it returns an array of bytes that contains the initial contents as the array elements in the bytearray object.

Mandatory condition: If an iterable is passed as an argument to the bytearray() function, it is necessary for all the elements of the iterable to be of type integer to avoid TypeError.

Example: Python bytearray() with list

inp_lst = [2, 4, 6, 8]
res_byte = bytearray(inp_lst)

As clearly understood, the contents of the list i.e. [2,4,6,8] have been used to create a bytearray object.



3. Python bytearray() function with integer value as an argument

If the bytearray() function encounters an integer value as a parameter, it then creates a bytearray object with the size = integer value and initializes it with null values (‘\0’).


inp = 14
res_byte = bytearray(inp)

In the above example, a array object is created with the array size as ’14’ and initialized with null values.




  • Python bytearray() function returns a bytearray object i.e. generates an array of bytes of the type of source input provided to the function.
  • The bytearray() function can have an iterable, values, etc as parameters to it.
  • Whenever an iterable is passed the function, it is essential for the elements of the iterable to be of integer type only.


Thus, in this article, we have understood the working of the Python bytearray() method with various types of parameters.