Convert a Byte to String in Python (or vice-versa)

To convert a byte to string in python is pretty easy and straight forward, but it is very important to understand the differences between these two data types (scroll down to the examples to see the code directly).

Converting Bytes to String (Theory)

In python, a byte object is a series of bytes. The python byte object is what our machines (computing machines), can read and what can store (or write).

A String, contrarily to bytes, is an encoded form of bytes that is able to be stored in our machines. This encoding makes us, humans, able to read this representation of bytes in a readable manner.

Encoding of a byte can be done in several ways, the most used ones (and most common) are the ASCII and UTF-8 encoding modes. In Python 3, the default encoding method is UTF-8.

To make it simpler, here is a very simple diagram:

Simple diagram relation between bytes and strings in Python

Python functions to convert a byte to string and vice versa

You may be saying now, Okay, yeah, cool, but show me the python code

As always with Python, the code to convert between bytes and strings is very easy:

  • To convert a byte to string, we can simply use the str() function, which encodes the object (string) using the encoding type (by default is UTF-8):
    • str(object, encoding=’utf-8′, errors=’strict’)
    • This function returns a string to show in a readable format the object that is passed.
  • To convert a string to a byte, we have to decode the bytes object using the bytes() function, similarly to the previous function:
    • bytes(object, encoding=’utf-8′, errors=’strict’)
    • This function will return a byt

Examples

Python Function Bytes()

In this section I will be showing you how to convert a byte to string and vice versa. Using these two functions is very easy:

str1 = 'Welcome to ChiByte.Com"

# We can encode this string using Bytes():
arr_bytes = bytes(str1, encoding='utf-8')
print(arr_bytes)

Python Function Str()

This will print the array of bytes the string is made of. We can decode this array of bytes easily again using the str() function:

arr_bytes = bytes(str1, encoding='utf-8')

# We can decode this string using Str():
str_out = str(arr_bytes, encoding='utf-8')
print(str_out)

The output of the snippet code above will be:

‘Welcome to ChiByte.com

Changing Encoding Mode

In the example above we have specifically set the encoding mode to be UTF-8. This is unnecessary, as I said before, python uses this mode by default. We could have simply done:

# Encoding and decoding by default:
arr_bytes = bytes(str1)
str_out = str(arr_bytes)

If we wanted to encode or decode with another mode we could have set it in the arguments of the function:

# Encoding and decoding with other modes:
arr_bytes = bytes(str1, encoding='utf-32')
str_out = str(arr_bytes, encoding='utf-32')

I hope with this article you are able now to convert a byte to string in Python and vice versa! If you need any help, you can ping me below in the comments section 🙂

Leave a Comment