Python bytearray() function



The Python bytearray() function is a built-in function that returns a new array of bytes. It is a mutable sequence of integers in the range 0 <= x < 256. This function can convert a specified object into a bytearray object or it can create an empty bytearray object of the required size.

The byte array can be created from the following sources −

  • String − We can create a bytearray from a string by encoding it to bytes using "str.encode()".

  • Integer − If the source is an integer, an array with null value of specified size will be created.

  • Object − In this case, a read-only buffer is used to initialize the byte array.

  • Iterable − Creates an array of size equal to the length of iterable.

  • No source − If no source is specified, a byte array of size 0 is created.

Syntax

Following is the syntax of the Python bytearray() function −

bytearray(source)
or,
bytearray(source, encoding)
or,
bytearray(source, encoding, errors)

Parameters

The Python bytearray() function accepts three parameters, all of which are optional −

  • source − It represents an object such as a list, string, or tuple.

  • encoding − It represents the encoding of the passed string.

  • errors − It specifies the required action when encoding fails.

Return Value

The Python bytearray() function returns a new array of bytes.

Examples

In this section, we will see some examples of bytearray() function −

Example 1

The following example shows the usage of Python bytearray() function. Here we are creating an empty bytearray.

empByte_array = bytearray()
print("It is an example of empty bytearray:", empByte_array)

When we run above program, it produces following result −

It is an example of empty bytearray: bytearray(b'')

Example 2

In the code below, we are converting a given string into array of bytes. To do so, we use bytearray() function by passing string and encoding as parameter values.

byte_arrayStr = "Tutorials Point bytearray"
str_byte_array = bytearray(byte_arrayStr, 'utf-8')
print("Creating bytearray from string:")
print(str_byte_array)

Following is an output of the above code −

Creating bytearray from string:
bytearray(b'Tutorials Point bytearray')

Example 3

The code below demonstrates how to create a byte array of the specified size. We are going to pass the size and value as arguments to the bytearray() function.

size = 5
value = 1
new_byte_array = bytearray([value]*size)
print("Bytearray of the given size:")
print(new_byte_array)

Output of the above code is as follows −

Bytearray of the given size:
bytearray(b'\x01\x01\x01\x01\x01')

Example 4

In the code below a bytearray is created using bytearray() function. Then we modify its 8th character using the ord() function. Since a bytearray object is mutable, it can be modified easily.

byte_arr = bytearray('Tuorialspoint', 'utf-8')
print("Original bytearray:", byte_arr)
byte_arr[8] = ord('P')
print("Modified bytearray:", byte_arr)

Following is an output of the above code −

Original bytearray: bytearray(b'Tuorialspoint')
Modified bytearray: bytearray(b'TuorialsPoint')
python_built_in_functions.htm
Advertisements