Python bytearray()

Python bytearray() function returns a bytearray object that means it converts an object into bytearray objects, which is an array of given bytes.

The bytearray() method provides mutable sequence of objects in the range of 0 <= x < 256

If you want an immutable version, you can use the bytes() method.

bytearray() Syntax 

The syntax of the bytearray() method is:

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

bytearray() Parameters

The bytearray() method takes three optional parameters.

  • source (Optional) – Initializes the array of bytes
  • encoding (Optional) – in case if the source is a string, the encoding of the string.
  • errors (Optional) – An action to take if the encoding conversion fails.  

The source parameter can be of any below type as follows.

TypeDescription
StringConverts the given string into bytes using str.encode(). In the case of a string, you must also pass encoding as an argument and, optionally errors
IntegerCreates an array of provided size and initializes with null bytes
ObjectA read-only buffer of the object will be used to initialize the byte array
IterableCreates an array of size equal to the iterable count and initialized to the iterable elements. The iterable should be of integers and the range should be in between 0 <= x < 256
No source (arguments)An array of size 0 is created.

bytearray() Return Value

The bytearray() function returns an array of bytes of the given size.

Example 1: Array of bytes of given integer size

In the case of an integer, it creates an array of provided size and initializes with null bytes.

# size of array
size = 6

# bytearray() will create an array of given size
# and initialize with null bytes
arr = bytearray(size)

print(arr)

Output

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

Example 2: Array of bytes from a string

Converts the given string into bytes using str.encode(). In the case of a string, you must also pass encoding as an argument and, optionally, errors.

# string declaration
string = "Hello World !!!"

# string with encoding 'utf-8'
arr1 = bytearray(string, 'utf-8')
print(arr1)

# string with encoding 'utf-16'
arr2 = bytearray(string, 'utf-16')
print(arr2)

Output

bytearray(b'Hello World !!!')
bytearray(b'\xff\xfeH\x00e\x00l\x00l\x00o\x00 \x00W\x00o\x00r\x00l\x00d\x00 \x00!\x00!\x00!\x00')

Example 3: Array of bytes from an iterable list

Creates an array of size equal to the iterable count and initialized to the iterable elements. The iterable should be of integers, and the range should be in between 0 <= x < 256

Note: if you pass an integer value greater than 256, Python will throw ValueError: byte must be in range(0, 256) 

# list of integers
lst = [1, 2, 3, 4, 5]

# iterable as source
arr = bytearray(lst)

print(arr)
print("Count of bytes:", len(arr))

Output

bytearray(b'\x01\x02\x03\x04\x05')
Count of bytes: 5

Example 4: If no source is passed to bytearray()

If no source is passed into bytearray(), an array of size 0 is created.

# array of size 0 will be created

# iterable as source
arr = bytearray()

print(arr)

Output

bytearray(b'')
0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…