The AttributeError: ‘str’ object has no attribute ‘append’ error occurs when the append() attribute is called in the str object instead of the concatenation operator. The str object does not have the attribute append(). That’s when the error AttributeError: ‘str’ object has no attribute ‘append’ has happened.

The python string does not support append() attribute. when you call append() attribute in a string, the exception AttributeError: ‘str’ object has no attribute ‘append’ will be thrown.

The AttributeError in python is defined as an error that occurs when a reference is made to an unassociated attribute of a class or when an assignment is made with an unassociated attribute of a class. The AttributeError is raised when an invalid class attribute is used for reference or assignment.

In this article, we will find out about the python attribute error, what are all the different types of attribute error, the root cause of the attribute error, when this attribute error occurs, and how to fix this attribute error in python.



Different Attribute Error Variation

The attribute error AttributeError: ‘type’ object has no attribute ‘x’ is shown with the object type and the attribute name. The error message will be shown as below

AttributeError: 'str' object has no attribute 'append'
AttributeError: 'int' object has no attribute 'append'
AttributeError: 'long' object has no attribute 'append'
AttributeError: 'float' object has no attribute 'append'
AttributeError: 'bool' object has no attribute 'append'
AttributeError: 'NoneType' object has no attribute 'append'
AttributeError: 'complex' object has no attribute 'append'
AttributeError: 'module' object has no attribute 'append'
AttributeError: 'class' object has no attribute 'append'
AttributeError: 'list' object has no attribute 'size'
AttributeError: 'tuple' object has no attribute 'size'
AttributeError: 'set' object has no attribute 'size'
AttributeError: 'dict' object has no attribute 'size'


Exception

If the python interpreter throws an attribute error, the attribute error “AttributeError: ‘type’ object has no attribute ‘x’” will be shown as below. The attribute error will show the type of the object from which it is ejected and the name of the attribute.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(2)
AttributeError: 'str' object has no attribute 'append'
[Finished in 0.0s with exit code 1]


Root Cause

The python class is a collection of data and functionality. The object in python is an enclosed collection of data and functionality identified as a class variable. The attribute in python is the collection of class-related data and functionality. These attributes are available for all class objects. The Attribute error is thrown if an unassociated attributes are invoked in a class.

The dot operator is used to reference to a class attribute. The reference attribute is made with an attribute that is not available in a class that throws the attribute error in python. The assignment is made with an attribute that is not associated with the class, which will also cause the attribute error AttributeError: ‘str’ object has no attribute ‘append’.



How to reproduce this issue

If the unassociated attribute is referenced in the class, or if the assignment is made with the unassociated attribute of the class, the python interpreter throws the error of the attribute.

In the example below the python string invokes the append attribute. The python string does not support the attribute of the append. As a result, the python interpreter throws the attribute error.

Program

a = 'Hello'
a.append(' World')

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 2, in <module>
    a.append(' World')
AttributeError: 'str' object has no attribute 'append'
[Finished in 0.1s with exit code 1]


Solution 1

You are referring an attribute that may be relevant to other languages such as java, c++, dot net etc. Refer the python manual for the supported attributes of the class. If you’re keen on specific features in python, refer to the python manual for this.

In the example below a variable contains a “Hello” string. Append method to concatenate with another “world” string is invoked In python, two strings are concatenated by using the arithmetic addition operator. This attribute error is fixed by replacing the append attribute with the arithmetic addition operator.

Program

a = 'Hello'
a = a +' World'
print a

Output

Hello World


Solution 2

The append() attribute will work on list of items. The string variable should be converted to a list of strings. The append will work on string list. The example below will show how to append in a list.

Program

a = ['Hello']
a.append(' World')
print a

Output

['Hello', ' World']
[Finished in 0.0s]


Solution 3

The python variable should be checked for the list. if the variable is of type list, then call the append method. Otherwise, take the alternative path and ignore the append() attribute. The example below will show how to check the type of the variable and how to call append method.

Program

a = 'Hello'
if type(a) is list :
	a.append(' World')
print a

Output

Hello
[Finished in 0.0s]


Solution 4

The preferred approach is to use try except blocks. If an error occurs in unusual occurrences, then it is recommended to try except blocks. When an error occurs, the code to be except block will be executed. The error will be resolved in run time.

Program

a = 'Hello';
try :
	a.append(' World')
except :
	print 'error';
print a

Output

error
Hello
[Finished in 0.0s]


Solution 5

You may refer to an attribute that belongs to another class, or the visibility of the attribute is not visible in the class. Check the attribute available in the class and make sure that it has proper visibility, such as public or protected.

In the example below, the python file Employee.py contains an Employee class. The Employee class is imported in the test.py file. The Employee class contains an attribute called “id” that is available in public access scope. the object of the Employee class is used to reference the attribute “id”.

Employee.py

class Employee:
	id = 1

test.py

from Employee import Employee

emp = Employee();
print(emp.id);

Output

1



Leave a Reply