In this tutorial, we will learn about python bytearray() method and its uses.

Python bytearray() Method

The bytearray() method will return a form of bytearray object, an array of given objects, or create an empty bytearray object of the specified size.

bytearray() method returns a bytearray object which is  a sequence of mutable(which can be modified) integers in the range  0 <= x <256.

The syntax of bytearray() method is:

bytearray([source, encoding, errors)

Python bytearray() Method Parameters

bytearray() takes three optional parameters:

  • source (Optional) – A source to Initialize the array of bytes.
  • encoding (Optional) – Encoding of the string when the source is a string.
  • errors (Optional) – if the source is a string, it will take a specific action when encoding fails.

Let’s see some examples of the bytearray() method in python.

Example 1: Using bytearray() with an integer.

number = 4

print(bytearray(number))

Output:

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

Example 2: using bytearray() with a string.

string = "Python is Awesome."

# encoding the string with unicode 8
array = bytearray(string,'utf-8')
print(array)

# encoding the string with unicode 16
array = bytearray(string,'utf-16')
print(array)

The output will be as follows.

bytearray(b'Python is Awesome.')
bytearray(b'\xff\xfeP\x00y\x00t\x00h\x00o\x00n\x00 \x00i\x00s\x00 \x00A\x00w\x00e\x00s\x00o\x00m\x00e\x00.\x00')

Example 3:  Using bytearray() with an iterable list.

my_list = [1,2,3,4,5]

print(bytearray(my_list))

Output:

bytearray(b'\x01\x02\x03\x04\x05')

Rules of bytearray()

  • It will return an array of bytes of the given size and initialization value.
  • If it is an integer, it will create an empty bytearray object of the specified size.

 

Was this article helpful?
Share on: