How to convert Python string to bytes?

In this tutorial, we look at how to convert Python string to bytes. We look at all the various methods along with their limitations and caveats.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - Python String to Byte

Python String to Bytes:

Converting Python strings to Bytes has become quite popular after the release of Python 3. This is largely because a lot of file handling and Machine learning methods require you to convert them. Before we dive into how to convert them let us first understand what they are and how they are different.

In Python 2, string and bytes were the same typeByte objects; however after the introduction of Python 3 Byte objects are considered as a sequence of bytes, and strings are considered as a sequence of characters. In essence, strings are human-readable and in order for them to become machine-readable, they must be converted to byte objects. This conversion also enables the data to be directly stored on the disk.

The process of converting string objects to byte objects is called encoding and the inverse is called decoding. We look at methods to achieve this below.

Method to convert strings to bytes:

There are many methods that can be used to convert Python string to bytes, however, we look at the most common and simple methods that can be used.

Using bytes():

The bytes() method is an inbuilt function that can be used to convert objects to byte objects.

Syntax of bytes():

bytes(str, enc, error)

The bytes take in an object (a string in our case), the required encoding method, and convert it into a byte object. The bytes() method accepts a third argument on how to handle errors.

Let us look at the code to convert a Python string to bytes. The encoding type we use here is “UTF-8”.

#Using the byte() method

# initializing string 
str_1 = "Join our freelance network"

str_1_encoded = bytes(str_1,'UTF-8')

#printing the encode string 
print(str_1_encoded)

#printing individual bytes
for bytes in str_1_encoded:
    print(bytes, end = ' ')

The output is as follows:

b'Join our freelance network'
74 111 105 110 32 111 117 114 32 102 114 101 101 108 97 110 99 101 32 110 101 116 119 111 114 107

As you can see this method has converted the string into a sequence of bytes.

Note: This method converts objects into immutable bytes, if you are looking for a mutable method you can use the bytearray() method.

Using encode():

The encode() method is the most commonly used and recommended method to convert Python strings to bytes. A major reason is that it is more readable.

Syntax of encode():

string.encode(encoding=encoding, errors=errors)

Here, string refers to the string you are looking to convert.

Parameters:

  • Encoding - Optional. The encoding method you are looking to use. After Python 3, UTF-8 has become the default.
  • Error - Optional, A string containing the error message.

Code to convert a python string to bytes:

#Using the encode method

# initializing string 
str_1 = "Join our freelance network"

str_1_encoded = str_1.encode(encoding = 'UTF-8')

#printing the encode string 
print(str_1_encoded)

#printing individual bytes
for bytes in str_1_encoded:
    print(bytes, end = ' ')

The output is the same as above.

b'Join our freelance network'
74 111 105 110 32 111 117 114 32 102 114 101 101 108 97 110 99 101 32 110 101 116 119 111 114 107

Similar to the encode() method, the decode() method can be used to convert bytes to strings.

#Using the encode method

#initializing string 
str_1 = "Join our freelance network"

str_1_encoded = str_1.encode(encoding = 'UTF-8')

#printing the encode string 
print(str_1_encoded)

#decoding the string
str_1_decoded = str_1_encoded.decode()
print(str_1_decoded)

Output:

b'Join our freelance network'
Join our freelance network

Limitations and Caveats - Python String to Byte

  • Both the methods solve the same problem efficiently and choosing a particular method would boil down to one’s personal choice. However, I would recommend the second method for beginners.
  • The byte() method returns an immutable object. Hence, consider using the bytearray() if you are looking for a mutable object.
  • While using the byte() methods the object must have a size 0 <=x < 256.

27