Python bytes() Function

Instance

Returns an array of 5 bytes:

x = bytes(5)

Running Instance

Definition and Usage

bytes() Function Returns Byte Object

It can convert an object to a byte object, or create an empty byte object of a specified size.

The difference between bytes() and bytearray() is that bytes() returns an immutable object, while bytearray() returns a mutable object.

Syntax

bytes(x, encoding, error)

Parameter Value

Parameter Description
x

Resources used to create bytearray objects

If it is an integer, it will create an empty bytearray object of the specified size.

If it is a string, make sure that the encoding of the resource is specified.

encoding String Encoding
error Specify what to do if encoding fails.

Related Pages

Reference Manual:bytearray() Function