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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

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

The Python "AttributeError: 'str' object has no attribute 'decode'" occurs when we call the decode() method on a string that has already been decoded from bytes.

To solve the error, remove the call to the decode() method as the string is already decoded.

attributeerror str object has no attribute decode

Here is an example of how the error occurs.

main.py
my_str = 'bobbyhadz.com' # โ›”๏ธ AttributeError: 'str' object has no attribute 'decode'. Did you mean: 'encode'? decoded = my_str.decode('utf-8')

# You don't need to call decode() on strings

The issue in the code sample is that we are trying to decode a string object.

Decoding is the process of converting a bytes object to a string and since we already have a string, we don't have to call the decode() method on it.

To solve the error, remove the call to the decode() method.

main.py
my_str = 'bobbyhadz.com' print(my_str) # ๐Ÿ‘‰๏ธ "bobbyhadz.com"

dont call decode on strings

We don't have to call the decode() method on strings because all strings in Python are already decoded.

# Use a try/except statement to handle a possible AttributeError

If you aren't sure where you have a bytes object or a string, use a try/except statement to handle the possible AttributeError.

main.py
my_str = 'bobbyhadz.com' try: decoded = my_str.decode('utf-8') print(decoded) except AttributeError: pass # ๐Ÿ‘‰๏ธ This runs

using try except statement to handle possible attribute error

We try to decode the value and if it doesn't have a decode() attribute (it's not a bytes object), we handle the AttributeError.

You can also extract the logic into a reusable function.

main.py
def decode(string): try: decoded = string.decode('utf-8') return decoded except AttributeError: return string result = decode('bobbyhadz.com') print(result) # ๐Ÿ‘‰๏ธ bobbyhadz.com result = decode(b'bobbyhadz.com') print(result) # ๐Ÿ‘‰๏ธ bobbyhadz.com
Encoding is the process of converting a string to a bytes object and decoding is the process of converting a bytes object to a string.

# Encoding a string vs Decoding a bytes object

You can use the str.encode() method to go from str to bytes and bytes.decode() to go from bytes to str.

main.py
my_text = 'bobbyhadz.com' my_binary_data = my_text.encode('utf-8') print(my_binary_data) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' my_text_again = my_binary_data.decode('utf-8') print(my_text_again) # ๐Ÿ‘‰๏ธ 'bobbyhadz.com'

encoding string vs decoding bytes

The str.encode() method is the opposite of bytes.decode() and returns a bytes representation of the Unicode string, encoded in the requested encoding.

# Using bytes() and str() functions instead

You can also use bytes(s, encoding=...) and str(b, encoding=...).

main.py
my_text = 'bobbyhadz.com' my_binary_data = bytes(my_text, encoding='utf-8') print(my_binary_data) # ๐Ÿ‘‰๏ธ b'bobbyhadz.com' my_text_again = str(my_binary_data, encoding='utf-8') print(my_text_again) # ๐Ÿ‘‰๏ธ 'bobbyhadz.com'

using bytes and str functions instead

The str class returns a string version of the given object. If an object is not provided, the class returns an empty string.

The syntax for using the bytes class is the same, except that a b prefix is added.

Ever since Python 3, the language has used the concepts of text and binary data instead of Unicode strings and 8-bit strings.

# All text in Python is Unicode

All text in Python is Unicode, however, encoded Unicode is represented as binary data.

You can use the str type to store text and the bytes type to store binary data.

In Python 3 you can no longer use u'...' literals for Unicode text because all strings are now Unicode.

However, you must use b'...' literals for binary data.

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'bobbyhadz.com' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you will get the "AttributeError: str object has no attribute error".

Since decode() is not a method implemented by strings, the error is caused.

If the error persists, follow the instructions in my AttributeError: 'str' object has no attribute 'X in Python article.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev