Skip to main content

Python bytes() Function

In Python, bytes() function is used for returning a bytes object. This method returns an immutable bytes object initialisation with the given size and data. It is use to convert object into bytes object. It can also create empty bytes object of the specified size.

Syntax

bytes(source, encoding, errors)

Parameter Value:

  • source:It is used to initialise the bytes array.
  • encoding:It is used for encoding of string.
  • errors:It is used to specify what to do if the encoding fails.

Return:

  • This method returns the byte object.

Example:

print(bytes(2))
print(bytes("Hello!!",'utf-8'))

Output:

b'\x00\x00'
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.