How to Solve Python AttributeError: ‘str’ object has no attribute ‘decode’

by | Programming, Python, Tips

In Python 2, a string is an array of bytes, like bytes in Python 3. To get a Unicode string, you can call string.decode(). However, literal strings are Unicode by default in Python 3, and you do not need to decode them. If you try to decode a string in Python 3, you will raise the AttributeError: ‘str’ object has no attribute ‘decode’.

This tutorial will go through the error in detail and how to solve it with code examples.


AttributeError: ‘str’ object has no attribute ‘decode’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object.

In Python, encoding is the process of converting a str to a bytes object and decoding is the process of converting a bytes object to a str. We can see a visual representation of encoding and decoding below:

encoding and decoding in Python
Encoding and Decoding in Python

Python 3 strings do not have decode as an attribute because they are already Unicode strings by default.

Example

Let’s look at an example of a string in Python 2.7. We will import the sys module to confirm the version of Python. Then, we will print the string and its type to the console.

import sys

print(sys.version)

string = "Côte d'Ivoire"

print(string)

print(type(string))
2.7.16 |Anaconda, Inc.| (default, Sep 24 2019, 16:55:38) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
Côte d'Ivoire
<type 'str'>

We can see from the above printout that the string is type str, which in Python 2 is raw bytes. We can decode raw bytes to a Unicode string using the decode() method. Let’s look at how to do this:

decoded = string.decode('utf-8')

print(decoded)

print(type(decoded))
u"C\xf4te d'Ivoire"

<type 'unicode'>

We end up with the decoded Unicode string, which has the code phrase for ô, \xf4.

Now let’s try to decode the same string with Python 3.

import sys

print(sys.version)

string = "Côte d'Ivoire"

print(string)

print(type(string))
3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]
Côte d'Ivoire
<class 'str'>

Python 3 has a built-in string class named str. String literals are objects of the str class. Let’s try to decode the string using the decode() method:

decoded = string.decode('utf-8')
print(decoded)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 decoded = string.decode('utf-8')
      2 print(decoded)

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

We throw the AttributeError because String literals are Unicode strings by default, and therefore, we do not need to decode them.

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: ‘str’ object has no attribute ‘decode’ occurs when trying to decode a literal string in Python 3. Python 3 strings are Unicode strings by default.

For further reading on encoding and decoding in Python, go to the article: What is the ‘u’ Before a String in Python?

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee