Python bytearray() function

Published February 15, 2022

In python programming, the bytearray() function is used to return the bytearray object. As its name implies, a Bytearray is simply a  group or collection of bytes. Using this function, you can either convert an object into a bytearray object or just construct an empty bytearray object of a specific size.

 

The Syntax

The following is the  simple syntax  used when using the bytearray() function:

bytearray(source, encoding, errors)

where:

  • the source is used to initialize  the array of the bytes

  • encoding – encodes the string

  • errors- perform a specific action when an action fails.

 

Example

In the following example below, we are going to create a bytearray from an integer.

size = 5

array1 = bytearray(size)

print(array1)

Output

If you run the above code, the following will be displayed.

 

Python byteArray function

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

251 Views