Skip to main content

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
Submitted by devanshi.srivastava on July 28, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.