Javatpoint Logo
Javatpoint Logo

Python bytes() Method

Python bytes() method is used to get byte array. This method takes three arguments and returns immutable sequence of integers. Bytes objects can also be created with literals but method gives a nice approach. The signature of the function is given below.

Signature

Parameters

Source: A source value to convert into an array of bytes.

Encoding: if the source is string, must pass it's encoding.

Error: set error for the source.

Return

It returns a byte array.

Let's see some examples of bytes() method to understand it's functionality.

Python bytes() Method Example 1

This example returns bytearray of size 2 and another byte array from the string.

Output:

b'\x00\x00'
b'hi'

Python bytes() Method Example 2

We can also pass list or dict also to the method. See the example below.

Output:

b'\x01\x02'
b'\x01\x03\x05\x07\t'

Next TopicPython Functions




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA