The bytearray() method returns the bytearray object, which is an array of given bytes. It gives a mutable sequence of integers in the range 0 <= x < 256.

Python bytearray Example

Python bytearray() is an inbuilt method that returns the bytearray object which is an array of the given bytes. The bytearray() function returns a bytearray object.

It can convert  objects into bytearray objects, or create an empty bytearray object of the specified size.

There are some rules followed by the bytearray() function depending on the type of source.

  1. If there is no argument is passed, an empty byte array is returned.
  2. If the source is the integer, it initializes the byte array of given length with null values.
  3. If the source is a string, encoding is mandatory and used to convert string to a byte array.
  4. If the source is iterable, such as list, it must be iterable of the integers in that range of 0 <= x < 256, which are used as initial content of the array.

See the following syntax of the bytearray() function.

#python #python bytearray #bytearray

Python bytearray: How to Use bytearray() Function
2.25 GEEK