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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'str' object has no attribute 'append'" occurs when we try to call the append() method on a string (e.g. a list element at specific index). To solve the error, call the append method on the list or use the addition (+) operator if concatenating strings.

attributeerror str object has no attribute append

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ AttributeError: 'str' object has no attribute 'append' my_list[2].append('d')

We accessed the list element at index 2 before calling the append() method.

We ended up calling append on a string (a list item), instead of the list, which caused the error.

To solve the error, call append() on the list instead.

main.py
my_list = ['a', 'b', 'c'] my_list.append('d') print(my_list) # 👉️ ['a', 'b', 'c', 'd']

The list.append() method adds an item to the end of the list.

The method returns None as it mutates the original list.

If you are trying to concatenate strings, use the addition operator (+) instead.

main.py
str_1 = 'hello' str_2 = 'world' result = str_1 + ' ' + str_2 print(result) # 👉️ 'hello world'

The addition (+) operator can be used to concatenate strings.

Alternatively, you can use a formatted string literal.

main.py
str_1 = 'hello' str_2 = 'world' result = f'{str_1} {str_2}' print(result) # 👉️ 'hello world'
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

If you are trying to append an element to the end of a list, you have to figure out how the value you are calling append() on got assigned a string.

main.py
my_list = ['a', 'b', 'c'] # 👇️ reassigned variable to string by mistake my_list = 'hello' # ⛔️ AttributeError: 'str' object has no attribute 'append' my_list.append('d')

We set the my_list variable to a list initially, but it later got set to a string, which caused the error.

You have to track down where the variable you are calling append() got assigned a string and correct the assignment.

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 = 'hello world' # [ '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 classes' attributes, and recursively of the attributes of its bases.

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

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

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.