Python – Convert Bytes To String Quickly

Are you looking to Convert Bytes to String in Python 3? In this article, I will show you how you can convert the bytes to String in Python Quickly.

You can convert bytes into a string easily using the decode method. For example, if you encode a string ‘abcd’ into bytes then you can easily use the decode method on that particular string which is now in bytes to get the string out of bytes.

Covert Bytes to String Using Decode Method

In a builtins package of Python 3, the decode method is defined in String Class that can be used to convert the encoded string of bytes into a string. Let us see the below example code to see the usage of the decode method for better understanding.

Return a string that has been decoded from the bytes that were sent in. The encoding used by default is ‘utf-8’. You may get UnicodeError if the decode method is not used properly on the Bytes.

#Initialzing a String in Bytes
bytes = b'abcd'

#Converting Bytes to String
strVal = bytes.decode()

#Printing Converted String
print(strVal)

Output:

abcd

Using Str Constructor to Decode the Bytes to String

If you have Byte data and you want to convert those to a string value then you can use the constructor of the String class as class str(object=b'', encoding='utf-8', errors='strict').

The constructor will return a valid decoded string for bytes that are encoded using ‘utf-8’. Str() returns the informal string representation in the first scene when a bytes object is passed without the encoding or errors arguments.

Let us see the below example code the usage of the str constructor to convert bytes to string.

#Initialzing a String in Bytes
bytes = b'coduber'

#Converting Bytes to String 
#using str Constructor
strVal = str(bytes, 'utf-8')

#Printing Converted String
print(strVal)

Output:

coduber

Make sure to mention encoding type as ‘utf-8’ in the constructor else your string object will remain as in bytes and once you will print it you will get output in bytes.

Python - Convert Bytes To String Quickly

Using Join and Map Characters

If you are having an array of the Byte values and you are looking to convert them into a string then you need to use the join method of String Class and Map object for character conversion.

Using Join Method you can decode the bytes of string into string immediately from the list of bytes provided. Let us see that in the below example code.

#Initialzing a String in Bytes
bytes = [67,79,68,85,66,69,82]

#Converting Bytes to String 
#using str Constructor
strVal = "".join(map(chr, bytes))

#Printing Converted String
print(strVal)
CODUBER

As you can see we have a List of Byte and we have converted that into a string using Map and Join Method.

Using Bytes Constructor and Decode Method

You can use the bytes constructor to construct the byte values of the given integer list in the above example and then decode those using the Decode method and the ASCII as encoding method that makes this conversion a lot faster.

#Initialzing a String in Bytes
givenBytesList = bytes([67,79,68,85,66,69,82])

#Converting Bytes to String 
#using str Constructor
strVal = givenBytesList.decode('ascii')

#Printing Converted String
print(strVal)

Output:

CODUBER

As you saw in the above code we successfully converted the converted list of integers into bytes to string using the ASCII encoding method.

Wrap Up

I hope you got the answer related to how you convert bytes to string in Python. I covered almost 4 ways for this answer and you can choose any one of them that works for your current requirement.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python Print 2 Decimal Place Quickly
  2. How To Add Numbers Using For Loop in Python
  3. How Can I Add New Keys To Dictionary Python
  4. Finding The Index Of An Item In A List Python
  5. How to Split a List in Python – 3 Ways

Leave a Reply

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