Python

Python Menu

Python

Python bytes() Function - Definition, Syntax, Parameters, Examples

Definition

The bytes() function returns a bytes object.

It can convert objects into bytes objects, or create empty bytes object of the specified size.

The difference between bytes() and bytearray() is that bytes() returns an object that cannot be modified, and bytearray() returns an object that can be modified.

Syntax

bytes(x, encoding, error)

Parameters

Parameter Description
x A source to use when creating the bytes object.
If it is an integer, an empty bytes object of the specified size will be created.
If it is a String, make sure you specify the encoding of the source.
encoding The encoding of the string
error Specifies what to do if the encoding fails.

Examples

result = bytes(16) print(result)

Introduction

Python Basics

Python Advance

Data Science Python Tutorials

Python Functions and Methods