The bytes()
returns a bytes object which is an immutable sequence of single bytes often used to store raw bytes of data.
Syntax:
bytes(source, encoding = 'utf-8', errors='strict')
The source
parameter can be an iterable of integers, a buffer object, a string, or an integer. It specifies the data to be converted to a bytes object. If the source
is not provided, the function returns an empty bytes object.
The encoding
parameter is an optional string that specifies the encoding to use if the source
parameter is a string. If not provided, the default encoding is 'utf-8'
.
The errors
parameter is an optional string that specifies how to handle encoding/decoding errors if the source
parameter is a string. The default value is 'strict'
, which raises a UnicodeDecodeError
or UnicodeEncodeError
on errors. Other possible values include 'ignore'
, 'replace'
, 'xmlcharrefreplace'
, and more.
bytes object in python are prefixed with a 'b'
bytes()
//b''
bytes([65, 66, 67])
//b'ABC'
bytes('Hello, world!', 'utf-8')
//b'Hello, world!'
bytes(10)
//b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'