TypeError: decoding str is not supported in Python is an error you may encounter when programming. The following article will show the cause and some ways to handle the error, such as setting parameters to the function correctly, using the addition operator to concatenate strings, the str.format(), and f-string functions. Post details below.
What causes the TypeError: decoding str is not supported in Python?
The “TypeError: decoding str is not supported” occurs when the second argument of the str() function is passed, the first argument must be a bytes object, or the same error also occurs when the second and third arguments are passed to the str() function.
Example:
firstStr = 'learnshareit' # Use the str() function newStr = str(firstStr, encoding='utf-8') print(newStr)
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
TypeError: decoding str is not supported
The error here is that when you pass the second argument to the encoding=’utf-8′ function, the first argument will have to be a bytes object.
The same error occurs when you put the 2nd and 3rd parameters as the str() function.
Example:
firstStr = 'learnshareit' # Use the str() function newStr = str(firstStr, str('website')) print(newStr)
Output:
Traceback (most recent call last):
File "./prog.py", line 4, in <module>
TypeError: decoding str is not supported
How to solve the TypeError: decoding str is not supported in Python?
The first parameter must be a bytes object if the second argument is passed
As above, the example causes an error if the second parameter encoding=’utf-8′ is passed to the str() function. The first argument must be a bytes object.
Example:
# Create a bytes object bytesObject = b'learnshareit' # Use the str() function to return a string newStr = str(bytesObject, encoding='utf-8') print('The bytes object converts to a string:', newStr)
Output:
The bytes object converts to a string: learnshareit
Use the addition operator to concatenate strings
You can use the addition operator to join strings together.
Example:
# Create a bytes object bytesObject = b'learnshareit' # Use the str() function to return a string firstStr = str(bytesObject, encoding='utf-8') # Use the addition operator to concatenate two strings secondStr = ' website' print('Result after concatenating two strings:', firstStr+secondStr)
Output:
Result after concatenating two strings: learnshareit website
Use the str.format() function
Syntax:
str.format(value1, value2, ….)
Parameter:
- value: value to be formatted.
The format() method returns the formatted result of a given value specified by the specified formatting.
Example:
- Use the str.format() to concatenate two strings.
- Alternate fields {} receive values from the format() function argument, then format them in the specified format and return the result to a string.
strToInsert = ' website' # Use the str.format() function to concatenate two strings result = ('learnshareit{}'.format(strToInsert)) print('Result after concatenating two strings:', result)
Output:
Result after concatenating two strings: learnshareit website
Use f-string
Syntax:
f'a{value:pattern}b'
Parameters:
- f character: use the string f to format the string.
- a,b: characters to format.
- {value:pattern}: string elements need to be formatted.
- pattern: string format.
Example:
- Use the f-string to concatenate two strings.
firstStr = 'learnshareit' strToInsert = ' website' # Use the f-string to concatenate two strings result = f'{firstStr}{strToInsert}' print('Result after concatenating two strings:', result)
Output:
Result after concatenating two strings: learnshareit website
Summary
Hope the article gives you an idea to fix the TypeError: decoding str is not supported in Python. Please comment below if you have any questions, and we will try to answer them. Thanks for reading.
Maybe you are interested:
- TypeError: ‘zip’ object is not subscriptable in Python
- TypeError: ‘_io.TextIOWrapper’ object is not subscriptable
- Argument of type builtin_function_or_method is not iterable

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java