How can I convert bytes to a Python string?


In this article, we will show you how to convert bytes to a string in python. Below are the various methods to accomplish this task −

  • Using decode() function

  • Using str() function

  • Using codecs.decode() function

  • Using pandas library

Using decode() function

The built-in decode() method in python, is used to convert bytes to a string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input byte string data.

  • Print input data.

  • Use the type() function(returns the data type of an object) to print the type of input data.

  • Use the decode() function to convert the input bytes to a python string.

  • Print output data.

  • Print the type of output data after conversion of input bytes to a python string using the type() function.

Example

The following program converts the input bytes to a python string using the decode() function –

# input bytes inputData = b'TutorialsPoint' # printing input bytes data print('Input data:', inputData) # printing the type of input data print(type(inputData)) # converting the input bytes to a python string using decode() function result = inputData.decode() # printing the result after decoding the bytes print('Output data:', result) # printing the type of output data(result) after conversion print(type(result))

Output

On executing, the above program will generate the following output −

Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>

Using str() function

The str() function returns the string format of the object i.e converts it into string form.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input byte string data.

  • Print input data.

  • Use the type() function(returns the data type of an object) to print the type of input data.

  • Use the str() function to convert the input bytes to a python string by passing the input data, 'UTF-8' as arguments to it.

  • Print output data.

  • Print the type of output data after conversion of input bytes to a python string using type() function.

Example

The following program converts the input bytes to a python string using the str() function −

# input bytes inputData = b'TutorialsPoint' # printing input bytes data print('Input data:', inputData) # printing the type of input data print(type(inputData)) # converting the input bytes to a string using str() function result = str(inputData, 'UTF-8') # printing the result after decoding the bytes print('Output data:', result) # printing the type of output data(result) after conversion print(type(result))

Output

On executing, the above program will generate the following output −

Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>

Using codecs.decode() function

The codecs module can be used to convert byte data types to strings as well.

The decode() method of the codecs module decodes the binary string into normal form.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the codecs module.

  • Create a variable to store the input byte string data.

  • Print input data.

  • Use the type() function(returns the data type of an object) to print the type of input data.

  • Use the decode() function of the codecs module to convert the input bytes to a python string.

  • Print output data.

  • Print the type of output data after conversion of input bytes to a python string using type() function.

Example

The following program converts the input bytes to a python string using codecs.decode() function −

# importing codecs module import codecs # input bytes inputData = b'TutorialsPoint' # printing input bytes data print('Input data:', inputData) # printing the type of input data print(type(inputData)) # converting the input bytes to a python string using # codecs.decode() function result = codecs.decode(inputData) # printing the result after decoding the bytes print('Output data:', result) # printing the type of output data(result) after conversion print(type(result))

Output

On executing, the above program will generate the following output −

Input data: b'TutorialsPoint'
<class 'bytes'>
Output data: TutorialsPoint
<class 'str'>

Using pandas library

Syntax

str.decode()

By calling the str.decode() function on a column, you can quickly convert bytes in a data frame that you are working within pandas into strings.

By default, the Python character encoding is usually UTF-8.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Use the import keyword to import the pandas module.

  • Create a variable to store the input dictionary and give some random key-value pairs to it. (Here we passed bytes as values)

  • Create the pandas dataframe of the above input dictionary using the DataFrame() function.

  • Convert the values of the clothes column to string(from bytes) to string using decode() function and store this result dataframe in a variable.

  • Print the resultant dataframe after converting the values from bytes to string

Example

The following program returns the pandas dataframe after converting the values of it from bytes to string –

# importing pandas module import pandas as pd # input dictionary inputDict = {'clothes' : [ b'shirt', b'pant', b'tshirt', b'cap']} # Creating pandas dataframe of the above dictionary dataframe = pd.DataFrame(data=inputDict) # Converting the dictionary values(bytes) to string result = dataframe['clothes'].str.decode("utf-8") # printing the resultant dataframe after converting the values from bytes to string print(result)

Output

On executing, the above program will generate the following output −

0 shirt
1 pant
2 tshirt
3 cap
Name: clothes, dtype: object

Conclusion

We learned how to convert bytes to Python strings using four different methods in this article. We also learned how to convert a python dictionary into a pandas dataframe. We learned how to use the decode() function to convert column values to strings.

Updated on: 28-Oct-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements