AttributeError: 'str' object has no attribute 'append'
Youβre calling a method or accessing a property that doesnβt exist on that type.
Fix 1: Check the type
# β Strings don't have .append()
name = "Alice"
name.append(" Smith") # AttributeError!
# β
Use the right method for the type
name = "Alice"
name = name + " Smith" # Strings use concatenation
# Lists have .append()
items = ["Alice"]
items.append("Bob")
Fix 2: Check for typos
# β Typo
my_list.apend("item")
# β
Correct spelling
my_list.append("item")
Fix 3: Variable is None
# β Function returned None
result = my_list.sort() # .sort() returns None!
result.append("item") # AttributeError!
# β
.sort() modifies in place
my_list.sort()
my_list.append("item")
Common methods that return None: .sort(), .append(), .extend(), .reverse(), .clear().
Fix 4: Wrong import or module
# β Imported the module, not the class
import json
data = json.loads(text)
data.loads(more_text) # dict has no .loads()!
# β
json.loads() is on the module, not the result
more_data = json.loads(more_text)
Debugging tip
# Check what attributes exist
print(type(my_var))
print(dir(my_var))
See also: Python cheat sheet | Python NoneType not subscriptable fix
Related: Pip Install Error Fix