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

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusion In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how we can append […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusion In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. In Python, we […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusion What are the types of arrays? An array is a common structure that stores multiple […]

  • 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 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.