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.

Syntax:

bytearray(source_input, encoding_scheme, error)

Parameters:

  • 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()
print(inp)

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

Output:

bytearray(b'')

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') 

print(arr_8) 

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

Output:

bytearray(b'JournalDev')

Example 2:

inp_str = "JournalDev"

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

print(arr_16) 

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

Output:

bytearray(b'\xff\xfeJ\x00o\x00u\x00r\x00n\x00a\x00l\x00D\x00e\x00v\x00')

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)
print(res_byte)

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

Output:

bytearray(b'\x02\x04\x06\x08')

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’).

Example:

inp = 14
res_byte = bytearray(inp)
print(res_byte)

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

Output:

bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')

Summary

  • 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.

Conclusion

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


References