Python bytearray example
The “bytearray” type is a mutable sequence of bytes that can be used to manipulate binary data. In Python, the “bytearray()” method is used to retrieve a “bytearray” object that lies in the range of “0” to “256”. This write-up presents a detailed guide on Python “bytearray()” method by covering the following contents:
- What is the “bytearray()” Method in Python?
- Applying the “bytearray()” Method to Retrieve Arrays of Bytes From the Specified Value.
- Applying the “bytearray()” Method to Retrieve Arrays of Bytes From Byte Object.
What is the “bytearray()” Method in Python?
The “bytearray()” method in Python returns a “bytearray” object. A “bytearray” object is a changeable integer sequence ranging from “0” to “256”. This means that each element of a “bytearray” object can be a single byte or a character.
Syntax
Parameter
In the specified syntax:
- The “source” parameter/attribute can be a string, a bytes object, an integer, or an iterable object.
- The “encoding” parameter specifies the encoding to use when converting a string to a “bytearray” object.
- The “errors” parameter specifies what to do if the encoding fails.
Multiple Scenarios of “source” Parameter
There are several ways to initialize the array with the “source” parameter, as follows:
- If the “source” parameter is a “string”, the bytearray object will be initialized with the bytes of the string.
- If the “source” parameter/attribute is a specified “bytes” object, the bytearray object will be initialized with the bytes of the bytes object.
- If the “source” parameter is an “integer”, the bytearray object will be initialized with an empty bytearray of the specified size.
- If the “source” parameter is an “iterable” object, the bytearray object will be initialized with the bytes of the iterable object.
Return Value
The retrieved value of the “bytearray()” method is a “bytearray” object.
Example 1: Applying the “bytearray()” Method to Retrieve Arrays of Bytes From the Specified Value
The below code is used to retrieve the “bytearray” object from the specified value:
print(bytearray(string_value, 'utf-8'), '\n')
int_value = 10
print(bytearray(int_value), '\n')
list_1 = [20, 15, 35, 95]
print(bytearray(list_1))
In the above code:
- The “string”, “integer”, and “list” are initialized in the code.
- The “bytearray()” method takes the specified value as an argument and retrieves the corresponding byte array object in each case.
Output
The byte-array object of the string, integer, and list are shown in this output.
Example 2: Applying the “bytearray()” Method to Retrieve Arrays of Bytes From Byte Object
The below example code is used to apply the “bytearray()” method to return arrays of bytes from byte objects:
print(arr1)
In this code, the “bytearray()” method takes the “byte” object as an argument and retrieves the byte-array object.
Output
The byte-array object has been returned appropriately.
Conclusion
In Python, the built-in “bytearray()” method is utilized to retrieve a bytearray object which is an array of specified bytes. This method returns a changeable sequence of integers in the “0” to “256” range. The “bytearray()” method can be utilized to retrieve arrays of bytes from string, integer, iterable, and byte objects. This tutorial presented a comprehensive guide on Python’s “bytearray()” method using several examples.
Source: linuxhint.com