Python bytes() function

Published February 15, 2022

This function returns a bytes object. Using this function, you can convert objects into bytes or create a new byte of a specified size. Bytearray() and bytes() functions are distinguished by the fact that bytearray() returns objects that can be modified while bytes() returns unmodifiable objects.

Syntax

Bytes(source, encoding, error)

Where:

  • Source- the source of the object to be   converted

  • Encoding- encodes the string objects

  •  Error- handles all errors that occur   during the file string conversions.

Example

str = "I love Python"

arr = bytes(str, 'utf-8')

print(arr)

Output

Pythin bytes function

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

224 Views