Last updated: Apr 8, 2024
Reading time·3 min
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.
Here is an example of how the error occurs.
a_str = 'bobbyhadz' # ⛔️ AttributeError: 'str' object has no attribute 'append' a_str.append('.com')
And here is another example of how the error occurs.
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.
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.
my_list = ['bobby', 'hadz', '.'] my_list.append('com') print(my_list) # 👉️ ['bobby', 'hadz', '.', 'com']
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.
str_1 = 'bobby' str_2 = 'com' result = str_1 + 'hadz.' + str_2 print(result) # 👉️ 'bobbyhadz.com'
The addition (+) operator can be used to concatenate strings.
Alternatively, you can use a formatted string literal.
str_1 = 'bobby' str_2 = '.com' result = f'{str_1}hadz{str_2}' print(result) # 👉️ 'bobbyhadz.com'
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.
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')
We set the my_list
variable to a list initially, but it later got set to a
string
which caused the error.
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.
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.
print('bobby hadz com'.split(" ")) # 👉️ ['bobby', 'hadz', 'com'] print('bobby,hadz,com'.split(",")) # 👉️ ['bobby', 'hadz', 'com']
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: