BlogsDope image BlogsDope

Python bytearray() function

Jan. 31, 2021 PYTHON FUNCTION 11218

One of the major advantages with Python programming language is that it consists of many useful built-in functions that make many operations quite simple to execute. One such method is the bytearray() method which is used to construct byte arrays in Python that are mutable in nature. We can specify the size of the output array and the function generates an array in which each element is only one byte in size. Since this function returns a bytearray object, it can be used to convert objects into bytearray objects, or create an empty bytearray object of the specified size.

Syntax

The following is the syntax of the bytearray() method:

​bytearray(source, encoding, error)

Parameters

The bytearray function takes the following parameters:
1. source: This parameter describes the source that is used to initialize the contents of the byte array.

The following objects can be passed as a source to the bytearray() function:

Source 
Details
String
The function converts string characters into bytes using str.encode() method. So when strings are provided as a source, a proper encoding format, as well as error, should be provided.
Integer
The method creates a byte array of size equal to the integer value and all elements initialized with a null value('\x00').
Object
The function uses a read-only buffer of the object to initialize the byte array.
Iterable
The iterable must have integers in the range of 0 <= x < 256 only. The method creates an array of size equal to the iterable size and each element initialized with the iterable elements.
No source
If no source parameter is passed then an array of size 0 is created.

2. encoding: This parameter is required if a string is passed as a source parameter. It can be any standard encoding parameter such as UTF-8, UTF-16, etc.

3. error: This parameter specifies the action to take in case the encoding process fails.

*Note: All the above three parameters are optional and if no parameter is passed, the function creates an empty byte array.

Return value of bytearray()

The bytearray() method returns an array of bytes of the specified size and initialized values according to the passed parameters. The returned byte array can be modified.

Examples

Passing Strings

st1 = "Python is easy."

# using 'utf-8' encoding on string
arr1 = bytearray(st1, 'utf-8')
print(arr1)

st2 = "Codesdope"
# using 'utf-16' encoding on string
arr2 = bytearray(st2, 'utf-16')
print(arr2)

Output:

bytearray(b'Python is easy.') bytearray(b'\xff\xfeC\x00o\x00d\x00e\x00s\x00d\x00o\x00p\x00e\x00')

Passing Integers

i = 10

# using integer as source
arr = bytearray(i)
print(arr)

Output:

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

Passing Objects

# using byte Object as source
arr = bytearray(b'xyz')
print(arr)
# a read-only buffer is created that initializes the byte array
for i in arr:
    print(i)

Output:

bytearray(b'xyz')
120
121
122  

Passing Iterables

# using iterable(list) as source
x = [1,2,3]
arr = bytearray(x)
print(arr)

# appending to byte array
arr.append(1)
print("After append:\n",arr)

# reversing byte array
arr.reverse()
print("After reverse:\n",arr)

Output:

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

After append:

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

After reverse: 

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

No argument

arr = bytearray()
print(arr)

Output:

​bytearray(b'') 

In this way we have learnt about the bytearray() function in Python, how it returns a mutable sequence of byte values that can be really helpful especially while dealing with binary data.


Liked the post?
Zealous Learner.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).