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

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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
a_str = 'bobbyhadz' # ⛔️ AttributeError: 'str' object has no attribute 'append' a_str.append('.com')

And here is another 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.

# Make sure to call the append() method on a list

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

main.py
my_list = ['bobby', 'hadz', '.'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']

call list append method on list

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

The method returns None as it mutates the original list.

# Use the addition (+) operator when concatenating strings

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

main.py
str_1 = 'bobby' str_2 = 'com' result = str_1 + 'hadz.' + str_2 print(result) # 👉️ 'bobbyhadz.com'

use addition operator when concatenating strings

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

Alternatively, you can use a formatted string literal.

main.py
str_1 = 'bobby' str_2 = '.com' result = f'{str_1}hadz{str_2}' print(result) # 👉️ 'bobbyhadz.com'
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}.

# Figure out where the variable got assigned a string

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 the variable to a string by mistake my_list = 'hello' # ⛔️ AttributeError: 'str' object has no attribute 'append' my_list.append('d')

figure out where the variable got assigned a string

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.

For example, calling the str.join() method on a list, joins the list into a string based on the provided separator.

main.py
a_list = ['bobby', 'hadz', 'com'] a_str = ','.join(a_list) print(a_str) # 👉️ bobby,hadz,com a_list = a_str.split(',') print(a_list) # 👉️ ['bobby', 'hadz', 'com'] a_list.append('new') print(a_list) # 👉️ ['bobby', 'hadz', 'com', 'new']

If you need to split the string into a list based on a separator, use the str.split() method and then call list.append().

The str.split() method splits the string into a list of substrings using a delimiter.

main.py
print('bobby hadz com'.split(" ")) # 👉️ ['bobby', 'hadz', 'com'] print('bobby,hadz,com'.split(",")) # 👉️ ['bobby', 'hadz', 'com']

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

# How to debug your code

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 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 append() 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.