Python bytearray()

Python bytearray() builtin function is used to create a bytearray object with from given data source or of given specific size.

In this tutorial, we will learn about the syntax of Python bytearray() function, and learn how to use this function with the help of examples.

Syntax

The syntax of bytearray() function is

</>
Copy
bytearray([source[, encoding[, errors]]])

where

ParameterRequired/OptionalDescription
sourceOptionalThe source can be an integer, string, iterable or an object that conforms to the buffer interface.
encodingOptional. Required if source is string,The encoding of the the string (if source is string).
errorsOptionalThe action to take if encoding fails.

Returns

The function returns object of class type bytearray.

Examples

1. bytearray(string, encoding)

In this example, we will give string value for source parameter and ‘utf-8’ for encoding, to the bytearray() function. The bytearray() function returns a bytearray object created from the string and encoding data.

Python Program

</>
Copy
source = "helloworld"
encoding = 'utf-8'
bytearray1 = bytearray(source, encoding)
print(f'Return Value: {bytearray1}')

Output

Return Value: bytearray(b'helloworld')

In this example, we will give string as source to the bytearray() function, but without encoding parameter.

Python Program

</>
Copy
source = "helloworld"
bytearray1 = bytearray(source)
print(f'Return Value: {bytearray1}')

Output

Traceback (most recent call last):
  File "d:/workspace/python/example.py", line 2, in <module>
    bytearray1 = bytearray(source)
TypeError: string argument without an encoding

2. bytearray(integer)

In this example, we will give integer for source parameter to the bytearray() function. The bytearray() function returns a bytearray object with size equal to given integer, and each item in the byte array as \x00.

Python Program

</>
Copy
source = 6
bytearray1 = bytearray(source)
print(f'Return Value: {bytearray1}')

Output

Return Value: bytearray(b'\x00\x00\x00\x00\x00\x00')

3. bytearray(list)

In this example, we will give list of integers as source to the bytearray() function. The bytearray() function returns a bytearray object created from the list of integers. The values in the list should be only integers with values ranging from 0 to 255.

Python Program

</>
Copy
source = [65, 66, 67, 68]
bytearray1 = bytearray(source)
print(f'Return Value: {bytearray1}')

Output

Return Value: bytearray(b'ABCD')

If any integer value in the list is out of range for a byte, then bytearray() throws ValueError, as shown in in the following program.

Python Program

</>
Copy
source = [65, 66, 67, 68, 362]
bytearray1 = bytearray(source)
print(f'Return Value: {bytearray1}')

Output

Traceback (most recent call last):
  File "d:/workspace/python/example.py", line 2, in <module>
    bytearray1 = bytearray(source)
ValueError: byte must be in range(0, 256)

Conclusion

In this Python Tutorial, we have learnt the syntax of Python bytearray() builtin function, and also learned how to use this function, with the help of Python example programs.