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

In Python, the “append()” function is used to add the element (strings or numeric) at the end of the list. However, the “append()” throws an error when used directly on the strings.

This write-up will provide a detailed guide on how to resolve this “AttributeError” using the following points:

So, let’s get started!

Reason 1: Calling append() Method on String

The major reason which causes this error in Python is when a user tries to access the append() method on a string. Here is an example:

The above output shows an error because the string value “John” is appended to another string using the “append()” function.

Solution 1: Use the Addition Operator “+” Instead of the append() Function

To resolve this error, we can concatenate the string using the addition operator “+” in a program. For example, the below code shows how we can concatenate two strings using this method:

Code:

str1 = "Lily"
output = str1 + ' John '
print(output)

In the above code, the first string “Lily” has been concatenated to the second string “John” using the addition operator “+” without any error.

Output:

Based on the above output, it is verified that both strings have been successfully concatenated.

Solution 2: Use the format() Function Instead of the append() Function

We can also use the “format()” function to join the strings in Python. The format() function is used for string formatting and places the variable value inside the string.

Code:

str1 = "Lily"
str2 = "John"
str3 = '{} {}'.format(str1, str2)
print(str3)

In the above code, the variable value of “str1” and “str2” has been placed inside the string value using format() function. The variable is passed as an argument of the format() function and the variable’s value uses curly braces to place inside the string.

Output:

The format() function successfully adds the variable value inside the strings.

Reason 2: Calling append() Method on Element of List Using Index Number

There is another reason which causes this error in Python when users try to apply the “append()” function on the string element of the list. Because the append() function is directly applied on strings, that is why it throws the following error:

The above output shows “AttributeError” because the append() function is applied on index position “0” of the input list.

Solution: Call the append() Method on the Complete List Instead of the Element

To resolve this error, we simply need to call the append() function on the complete list instead of the specific list element. The below-stated code explains how we can achieve that:

Code:

string_list = ["Lily", "John"]
string_list.append('David')
print(string_list)

In the above code, the “append()” function is applied on the list “string_list” and appends the string value at the end of the string.

Output:

The string value “David” has been appended to the end of the string using the append() method.

Conclusion

The error “str object has no attribute append” occurs when a user tries to use the “append()” function on the string or the strings element of the list. To resolve this error, different solutions are utilized in Python, such as using the addition operator “+” to add the strings, using the string format() function, etc. This article has explained different solutions to the error “str object has no attribute append” in Python using numerous examples.