The bytes()
function in Python returns a new 'bytes' object which is an immutable sequence of integers in the range 0 = x 256. It can take arguments in the form of integers, strings, or iterable objects and convert them to bytes. This function is commonly used when working with binary data or byte strings.= x 256. It can take arguments in the form of integers, strings, or iterable objects and convert them to bytes. This function is commonly used when working with binary data or byte strings.= x 256. It can take arguments in the form of integers, strings, or iterable objects and convert them to bytes. This function is commonly used when working with binary data or byte strings.
Parameter Values
Parameter | Description |
---|---|
source | The source can be an integer, a sequence, or a collection of integers that range from 0 to 256. |
encoding | The optional parameter 'encoding' specifies the encoding of the source string. Default is 'utf-8'. |
errors | The optional parameter 'errors' specifies how to handle errors in the encoding process. Default is 'strict'. |
Return Values
The bytes()
function returns an object of type bytes
.
How to Use bytes()
in Python
Return a new bytes
object, which is an immutable sequence of integers in the range 0 = x 256.
bytes([97, 98, 99])
If no argument is given, it returns an empty bytes
object.
empty_bytes = bytes()
When a string is passed as an argument, it encodes the string using the specified encoding.
encoded_bytes = bytes('hello', 'utf-8')