Convert Bytearray to String in Python

Convert Bytearray to String in Python

Python supports different types of sequence objects to store data. One such object is a bytearray object. As the name suggests, a bytearray object is an array of bytes or a sequence of bytes. In this article, we will discuss different ways to convert bytearray to string in Python.

How to create a bytearray from a string?

Before converting a bytearray to a string, let us first discuss how we can convert a string into a bytearray. After creating a bytearray object from a string, we will learn how to convert a bytearray to a string in the subsequent sections.

To convert a string to a bytearray we use the bytearray() constructor. The syntax for the bytearray() constructor is as follows.

byteArrayObject=bytearray(input_string, encoding_format, error_message)

Here,

  • The byteArrayObject is the output given by the bytearray() constructor.
  • input_string is the string that has to be converted into a bytearray.
  • encoding_format’ is the encoding format that we follow while converting a string to bytearray. Generally, it is ‘utf-8’ or ‘utf-16’.
  • error_message  is the message that is displayed when any error occurs during the execution of the bytearray() constructor. It is an optional argument.

To convert a string to a bytearray object, we can pass the input string and the encoding format to the bytearray constructor as follows.

Output:

As you can observe, we have created a bytearray object from a string. Now we will discuss different ways to convert a bytearray to string in python.

Using the str() Function to convert Bytearray to String in Python

We can convert a bytearray to a string using the str() function. Generally, we use the str() function to convert an integer, a floating-point number, or other value to a string as follows.

Here, we don’t need to specify the encoding format. But, If we want to convert a bytearray to string using the str() function, we also need to specify the encoding format as the second argument as shown below.

Output:

Here, the encoding format should be the same as the format used while creating the bytearray object. If we specify any other encoding format in the str() function, it will give undesired output. The program may also run into errors. You can observe this in the following example.

Output:

Here, you can see that the str() method couldn’t convert a bytearray object to a string because we didn’t pass the encoding format same as the one used while creating the bytearray object.

Using the decode() Function to convert Bytearray to String in Python

An alternative way to convert a bytearray to string is by using the decode() method. The decode() method, when invoked on a bytearray object, takes the encoding format as input and returns the output string. 

You can convert a bytearray to a string using the decode() method as follows.

Output:

Again, you have to specify the same encoding format that was used while creating the bytearray from the string. Otherwise, you will get undesired outputs as shown below.

Output:

Using the codecs Module to convert Bytearray to String in Python

An implementation of the decode() function is also given in the codecs module. You can also use the decode() function defined in the codecs module to convert a bytearray to a string in python.

Here, the decode() function takes the bytearray object as its first argument and the encoding format as its second argument. After execution, it returns the output string as shown in the following example.

Output:

Conclusion

In this article, we have discussed three ways to convert a bytearray object to a string in python. Here, you can use any approach to perform the operation. However, you should make sure that you choose the encoding format correctly. Otherwise, you won’t be able to get desired output.

That’s all about how to convert Bytearray to String in Python.

I hope you enjoyed reading this article. Happy Learning.

Related Posts

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Rotate image Python
    20 August

    Rotate image in Python

    Table of ContentsUsing the image.rotate() function from the Pillow Imaging Library (Fork) to rotate image in Python.Using NumPy library functions to rotate image in Python.Using the OpenCV and imutils library functions to rotate image in Python.Using matplotlib and SciPy libraries to rotate image in Python. Python supports the use of images and sometimes a given […]

  • 06 April

    Print Matrix in Python

    In this article you will learn "How to print a matrix in Python?"

  • 20 November

    How to initialize array in Python

    Table of ContentsUsing for loop, range() function and append() method of listIntialize empty arrayIntialize array with default valuesIntialize array with valuesUsing list-comprehensionUsing product (*) operatorUsing empty() method of numpy module In this article, we will see a different ways to initialize an array in Python. Array is collection of elements of same type. In Python, […]

  • 18 November

    Print Array in Python

    Table of ContentsPrint ListUsing print()Using map()By unpacking listUsing loopPrint Numpy-ArrayUsing print()Using loop In this post, we will see how to print array in Python. As we know that, Python didn’t have an in-built array data type, so we try to use list data type as an array. We can also use the NumPy module for […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.