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:
- Reason 1: Calling append() Method on String
- Solution 1: Use the Addition Operator β+β Instead of the append() Function
- Solution 2: Use the format() Function Instead of the append() Function
- Reason 2: Calling append() Method on Element of List Using Index Number
- Solution: Call the append() Method on the Complete List Instead of the Element
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.