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.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×