Python bytearray() Function

bytearray() method return a bytearray object which is an array of the given bytes. It is used to convert objects into bytearray objects. It can create empty bytearray object of the specified size. bytearray()  gives a mutable sequence of integers in the range 0 <= x < 256.

Syntax:

bytearray(source, encoding, errors)

Parameter Values:

  • source: It is used to initialisation of array of bytes.
  • encoding:It is used for encoding of string.
  • errors:It is used to take action when encoding fails.

Return:

  • It returns an array of bytes of the given size.

Example:

print(bytearray(3))
print(bytearray("Hello!!",'utf-8'))

Output:

bytearray(b'\x00\x00\x00')
bytearray(b'Hello!!')
Tags