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

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:
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.
Output:
Example 2: using bytearray() with a string.
The output will be as follows.
Example 3: Using bytearray() with an iterable list.
Output:
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.