Site icon i2tutorials

Bytes() method in Python Built in Function

Bytes() method in Python Built in Function

 

bytes method in built function returns the bytes as an object in the given function.This function is an immutable sequence of integers in the range of 0<=x<=256.

 

Syntax:

bytes(([source[, encoding[, errors]]]))

 

Parameters:

Bytes method has 3 parameters. These parameters are optional

 

Table for different source parameters

Type of sourceDescription
StringThis converts the string into bytes by using str.encode(). We have to provide encode and errors
IntegerThis creates an array of provided size
ObjectTo initialize the byte array read only buffer object is used
IterableIt creates an array whose size is equal to the Iterable count
No source(arguments)It creates an array with size 0

 

 

Example-1:

 

Byte method using integer

x = bytes(2)
print(x)

 

Output:

 

Example-2:

 

Conversion of string into bytes

x = "There are many colours in the Universe"
text= bytes( x ,'utf-8')
print(text)

 

Output:

 

Example-3:

 

Converting list into bytes

text = [4,5,6,7,8]
x= bytes( text)
print(x)

 

Output:

 

 

Exit mobile version