typeerror decoding str is not supported

In this article, we will discuss on how to solve the error typeerror: decoding str is not supported.

Also, we’ll discuss to you what are the causes and why this error occur?

Why the decoding str is not supported occur?

The decoding str is not supported typically occurs because when you try to decode a string that is already in the decoded state.

In other words, the string has already been converted from a byte sequence to a Unicode string, and you are trying to convert it again.

This is an example of how the error appears:

str('Welcome to the tutorial typeerror: decoding str is not supported ', str(158))

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 1, in
str(‘Welcome to the tutorial typeerror decoding str is not supported ‘, str(158))
TypeError: decoding str is not supported

In this example, the code attempts to concatenate two strings, yet mistakenly passes str(158) as an argument to the str() function.

This causes a typeerror decoding str is not supported because you can’t convert a string to a string.

Let’s take a look another example:

str('typeerror', encoding='utf-8')

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 1, in
str(‘typeerror’, encoding=’utf-8′)
TypeError: decoding str is not supported

In this example, The code str(‘typeerror’, encoding=’utf-8′) tries to convert the string ‘typeerror’ to bytes using the UTF-8 encoding.

However, the str() function does not support the encoding parameter.

Therefore, it results in a TypeError that says decoding str is not supported.

What are the causes of typeerror: decoding str is not supported?

The causes of typeerror: decoding str is not supported error usually occurs if you’re trying to decode a string object that cannot be decoded.

This error commonly occurs if you are trying to use the decode method on a string object that has already been decoded, or when you try to decode a string object that is not in a valid format.

How to solve this error?

There are two solutions to solve this error and the following are:

  • Make sure to supply a valid bytes object
  • You don’t have to decode strings

First solution: Supply a valid bytes object

We are only able to set the encoding when a valid bytes object is supplied.

For example:

print(str(b'Welcome to the tutorial decoding str is not supported ', encoding='utf-8'))

The output being displayed is:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Welcome to the tutorial decoding str is not supported

Code explanation:

The string that is printed is created using the bytes literal notation b'...' which creates a sequence of bytes.

This sequence of bytes is decoded into a string using the str() function.

It defined the encoding used to decode the bytes with the encoding parameter set to ‘utf-8’.

The resulting decoded string is printed to the console using the print() function.

In addition, you can use a formatted string literal.

Let’s see the other example:

# Assign a new string value to str_value
str_value = 'xyz'

number = 123

result = f'{str_value} {number}'

print(result)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
xyz 123

This code assigns a new string value ‘xyz’ to the variable str_value.

It will assign an integer value of 123 to the variable number.

The code then uses an f-string (formatted string literal) to concatenate the string value and the integer value with a space in between them.

The result of this concatenation is stored in the variable result.

Finally, the code prints the value of the result variable, which is the concatenated string ‘xyz 123’.

Second Solution: Don’t decode the strings

The “TypeError decoding str is not supported” error which is usually means that we are trying to decode a string.

Note: When the string has already been decoded from a bytes object, you must remove any code that will tries to decode it.

This is an example of encoding a string to bytes and for decoding the bytes object back to a string.


my_string = 'hello world'
my_bytes = my_string.encode('utf-8')


print(my_bytes)


my_decoded_string = my_bytes.decode('utf-8')


print(my_decoded_string)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
b’hello world’
hello world

Code explanation:

This code is to encode a string to bytes using UTF-8 encoding:

my_string = 'hello world'
my_bytes = my_string.encode('utf-8')

Next, this code is to print the encoded bytes:

print(my_bytes)

Then, this code below is to decode the bytes to a string using UTF-8 encoding:

my_decoded_string = my_bytes.decode('utf-8')

Finally, this code is to print the decoded string:

print(my_decoded_string)

Additional Resources

By reading the following tutorials you will be able to learn more about the Python error topics:

Conclusion

In conclusion, by following the solutions in this article you will be able to avoid to encounter the typeerror decoding str is not supported.

FAQs

What is the difference between encoding and decoding in Python?

Encoding is the process of converting a string to a bytes object while the Decoding is the process of converting a bytes object to a string.