How To Resolve AttributeError: ‘Str’ Object Has No Attribute ‘Decode’ In Python

AttributeError: 'str' object has no attribute 'decode'

To fix AttributeError: ‘str’ object has no attribute ‘decode’ in Python, there are some solutions we have: Remove decode() method, use the isinstance() function to check the object, downgrade your Python version to Python 2 and encode then decode the string object. Follow the article to see how to do it.

What causes the AttributeError: ‘str’ object has no attribute ‘decode’?

If you use Python built-in functions with wrong data types, the compiler will display an AttributeError.

From Python 3 version, when you work with the string, it is decoded. So the error occurs because you try to call the decode() method on the string.


myString = 'visit learnshareit website'
myDecode = myString.decode('utf-8')



Traceback (most recent call last):
  File "./", line 2, in <module>
    myDecode = myString.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

How to solve the AttributeError: ‘str’ object has no attribute ‘decode’?

Remove the decode() method

As I mentioned in the cause of the error, strings in Python 3.x are already decoded, so the simplest way to understand it is to remove the decode() method. Output the string directly.


myString = 'visit learnshareit website'

# Remove decode() method down directly to the string


visit learnshareit website

Use the isinstance() function to check the object

In order not to get this error while programming, it affects the work progress. You can use the isinstance() function to check whether an object is a string.


isinstance(object, classinfo)


  • object: the object you need to check.
  • classinfo: class, type, or tuple.


myString = 'visit learnshareit website'

if isinstance(myString, str):
	print('The object is a string.')
	print('The object is not a string.')


The object is a string.

Downgrade your Python version to Python 2

Downgrading the Python version is probably not recommended. Because it will lose valuable updates only available in Python 3, such as legacy code, clean code (less syntax), faster computing performance, and Unicode support,… But if you still decide, here’s your shot at downgrading to Python 2.

pip install 'h5py == 2.10.0' –force-reinstall

Encode then decode the string object

If you want to dig into details, you can use this method to satisfy your curiosity, but I think it’s not a good way. You initially have a string. You encode it to the ‘bytes’ type using the str.encode() function, then decode the ‘bytes’ object using the str.decode() function.


myString = 'visit learnshareit website'

# Use the encode() function to encode the string object
myEncode = myString.encode('utf-8')
print('String when encrypted: ', myEncode)
print('Data type after string encoding: ', type(myEncode))

# Use the decode() function to decode the string object
myDecode = myEncode.decode('utf-8')
print('String when decoded: ', myDecode)


String when encrypted: b'visit learnshareit website'
Data type after string encoding: <class 'bytes'>
String when decoded: visit learnshareit website


Hopefully, through the article, you have understood the problem and fixed the AttributeError: ‘str’ object has no attribute ‘decode’. The best way is to install Python and remove the decode() method. I think it’s already optional when you work with strings. Let me know your opinion.

Maybe you are interested:

Leave a Reply

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