Python bytearray()
Python bytearray() Class
The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0 <= x < 256.
Syntax
class bytearray([source[, encoding[, errors]]])
Parameter
source[optional]: This parameter is used to Initialize the given array of bytes.
encoding[optional]: This parameter encoding the specified string.
errors[optional]: The error parameter takes action when encoding fails.
Return
It returns an array of bytes of the specified size.
Example 1
# Python program explaining # bytearray() class inp_str = "HelloWorld" # encoding the string with unicode 8 and 16 inp_array1 = bytearray(inp_str, 'utf-8') inp_array2 = bytearray(inp_str, 'utf-16') print(inp_array1) print(inp_array2)
Output
bytearray(b'HelloWorld') bytearray(b'\xff\xfeH\x00e\x00l\x00l\x00o\x00W\x00o\x00r\x00l\x00d\x00')