The Python bytearray() function returns a new array of bytes. It is a mutable sequence of integers in the range 0 <= x < 256. This function can convert a specified object into a bytearray object or it can create an empty bytearray object of the required size. It is one of the built-in functions in Python.
The byte array can be created from the following sources −
String − We can create a bytearray from a string by encoding it to bytes using str.encode().
Integer − If the source is an integer, an array with null value of specified size will be created.
Object − In this case, a read-only buffer is used to initialize the byte array.
Iterable − Creates an array of size equal to the length of iterable.
No source − If no source is specified, a byte array of size 0 is created.
Following is the syntax of the Python bytearray() function −
bytearray(source) or, bytearray(source, encoding) or, bytearray(source, encoding, errors)Parameters
The Python bytearray() function accepts three parameters, all of which are optional −
source − It represents an object such as a list, string, or tuple.
encoding − It represents the encoding of the passed string.
errors − It specifies the required action when encoding fails.
The Python bytearray() function returns a new array of bytes.
bytearray() function ExamplesPractice the following examples to understand the use of bytearray() function in Python:
Example: Use of bytearray() FunctionThe following example shows the usage of Python bytearray() function. Here we are creating an empty bytearray.
empByte_array = bytearray() print("It is an example of empty bytearray:", empByte_array)
When we run above program, it produces following result −
It is an example of empty bytearray: bytearray(b'')Example: Convert String to bytearray Object Using bytearray()
In the code below, we are converting a given string into array of bytes. To do so, we use bytearray() function by passing string and encoding as parameter values.
byte_arrayStr = "Tutorials Point bytearray" str_byte_array = bytearray(byte_arrayStr, 'utf-8') print("Creating bytearray from string:") print(str_byte_array)
Following is an output of the above code −
Creating bytearray from string: bytearray(b'Tutorials Point bytearray')Example: Create bytearray of Specified Size Using bytearray()
The code below demonstrates how to create a byte array of the specified size. We are going to pass the size and value as arguments to the bytearray() function.
size = 5 value = 1 new_byte_array = bytearray([value]*size) print("Bytearray of the given size:") print(new_byte_array)
Output of the above code is as follows −
Bytearray of the given size: bytearray(b'\x01\x01\x01\x01\x01')Example: Modify bytearray
In the code below a bytearray is created using bytearray() function. Then we modify its 8th character using the ord() function. Since a bytearray object is mutable, it can be modified easily.
byte_arr = bytearray('Tuorialspoint', 'utf-8') print("Original bytearray:", byte_arr) byte_arr[8] = ord('P') print("Modified bytearray:", byte_arr)
Following is an output of the above code −
Original bytearray: bytearray(b'Tuorialspoint') Modified bytearray: bytearray(b'TuorialsPoint')
python_built_in_functions.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4