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