Convert Bytes to String in Python

Hello Python coder! In this tutorial, we will learn how to convert the Bytes to String in Python. We will explore multiple ways to achieve the same conversion!

Before any delay, let’s begin!

Introduction to Bytes Data in Python

To put things in simple words, a byte is a unit of data that consists of 8 bits. And in that sequence of 8 bits, each bit can be either a 0 or a 1. And the reason why we use them is because machine understands the language of 0s and 1s much more easily than human language. This fact makes Byte a much more convenient and efficient way to store data when it’s looked at from the computer’s perspective.

Before learning how to convert them to String, let’s first understand how to create them using Python. We can create and manipulate bytes in Python using the bytes data type as shown in the code snippet below leading literals :  b or B.

byteData = b'Code'
for i in byteData:
  print(i, end=" ")

In this code, we are also gonna iterate over the same byte data and see what result we get. Well, what we expect is “C o d e”. That seems obvious but have a look at the actual output we obtain below:

67 111 100 101

Surprising right? Let me explain what exactly is happening here. As I mentioned earlier, the byte is a way to store the data in numeric form to make it easily understandable by machines and hence instead of printing letters, we end up getting the ASCII values for each letter stored inside the byte data. For instance, the ASCII value of ‘C’ is 67, for ‘o’ it is 111, and so on.

I hope the concept of byte is clear to you now. Let’s move on to learning conversion of the byte data to strings in the next section but before we move on to code implementation, do consider the note that this tutorial is designed for Python 3.X version. If you have not installed the same on your system yet, please make sure you install the same.

Method 1 – Use decode Method

The very first and most commonly used method is decode() which takes an encoding as a parameter. Now what we mean by encoding is it points to the encoding that we want to decode. As I mentioned before the byte data represents the ASCII code of each letter at the memory level and hence, here we wish to decode the ascii values of byte data. Let’s see how that works using the code below:

stringData_1 = byteData.decode('ascii')
for i in stringData_1:
  print(i, end=" ")

Now when you execute this piece of code, you get the desired output that’s ‘C o d e’. But the code doesn’t end here, let’s explore some other approaches.

Method 2 – Use str Method

Another approach is making use of the in-built function called str which takes a particular object in a particular encoding and then decodes it into a string using the code below. For the encoding we will be using the same encoding that is ascii.

stringData_2 = str(byteData, 'ascii')
for i in stringData_2:
  print(i, end=" ")

The output for the code is similar to the previous approach and the final output comes out to be ‘C o d e’.

Method 3 – Using codecs module

If you wish to take the help of a library present in Python, one of them is the codecs library. This library is responsible for converting various objects into different forms. For converting the byte data encoded in ascii codes to textual data, we will achieve the same using the code below:

import codecs
stringData_3 = codecs.decode(byteData, 'ascii')
for i in stringData_3:
  print(i, end=" ")

Conclusion

Congratulations! You can now convert any data in bytes to strings within a few minutes in Python using various methods mentioned in this tutorial.

I hope you found this tutorial helpful and learned something new today.

Also Read:

  1. Convert a string to uppercase in Python
  2. Convert a string to a title case in Python

Happy Coding!

Also Read: Convert string to bytes in Python

Leave a Reply

Your email address will not be published. Required fields are marked *