AppDividend
Latest Code Tutorials

Python bytes: How to Use bytes() Function in Python

0

The bytes() function can convert objects into bytes objects, or create the empty bytes object of the specified size. The main difference between bytes() and byteArray() is that bytes() return an object that cannot be modified which means it returns an immutable object.

Python bytes()

Python bytes() is an inbuilt function that accepts a string as an argument and requires the encoding scheme with it to perform it. A most crucial aspect of this is handling errors in case of encoding failure. The bytes() method returns immutable bytes object initialized with the given size and data.

On the other hand, the byteArray() function returns the object that can be changed or altered.

Syntax

bytes(source, encoding, error)

Parameters

sourceIt is the source to use when creating the bytes object.

If it is the integer, an empty bytes object of the specified size will be created.

If it is the String, make sure you specify the encoding of the source.

encodingThe encoding of the string.
errorDetermines what to do if the encoding fails.

 

Return Value

The bytes() function returns the following.
Byte immutable object consisting of Unicode 0-256 characters according to src type.

  1. integer: Returns array of size initialized to null
  2. iterable: Returns array of iterable size with elements equal to iterable elements( 0-256 )
  3. string: Returns the encoded string acc. to enc and if encoding fails, performs an action according
    to err specified.
  4. No arguments: Returns array of size 0.

Okay, now we will see the following code example.

# app.py

enola = "Eleven is badass character"

data = bytes(enola, 'utf-8')
print(data)

See the following output.

➜  pyt python3 app.py
b'Eleven is badass character'
➜  pyt

#Use bytes() Function With Python List

Let’s define a Python List and then use the bytes() function on it.

See the following code.

# app.py

list = [11, 21, 18, 19, 29]

print("The iterable conversion results in : "  +str(bytes(list)))

See the following example.

➜  pyt python3 app.py
The iterable conversion results in : b'\x0b\x15\x12\x13\x1d'
➜  pyt

#Python bytes() Function With Integer

Okay, let’s work with integer and see the output.

# app.py

data = 11

print("The integer conversion results in : "  +str(bytes(data)))

See the following output.

➜  pyt python3 app.py
The integer conversion results in : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
➜  pyt

Create a byte of given integer size.

# app.py

data = 10

output = bytes(data)
print(output)

See the output.

➜  pyt python3 app.py
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
➜  pyt

#Python bytes to string

If we want to convert bytes to a string in Python, then you need to decode the bytes object to produce a string. See the following code.

# app.py

data = b"KRUNAL"

output = data.decode("utf-8")
print(output)

See the following output.

➜  pyt python3 app.py
KRUNAL
➜  pyt

Finally, Python bytes Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.