/errors/python/attributeerror-str-object-has-no-attribute-append
AttributeError: 'str' object has no attribute 'append'
- tool
- Python
- exit code
- 1
- versions
- Python 3.x
- os
- any
AttributeError: 'str' object has no attribute 'append' means the variable in front of .append(...) holds a string, not a list. Only lists have append. Look at the line named in the traceback, then find the last assignment to that variable: it replaced a list with a string, or never was a list.
Reproduce it
python3 - <<'PY'
names = "ann"
names.append("bob")
PYOutput · exit 1
Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: 'str' object has no attribute 'append'
python3 - <<'PY'
items = []
for word in ["a", "b"]:
items = word
items.append(word)
PYOutput · exit 1
Traceback (most recent call last): File "<stdin>", line 4, in <module> AttributeError: 'str' object has no attribute 'append'
The second example is the common bug: the loop rebinds items to a string, so the first iteration works on the string and fails. The text in quotes is the type of the object: the same message with 'list' object has no attribute 'split' or 'dict_keys' object has no attribute 'append' has the same cause and fix.
Cause 1: the variable started as a string
Initialising an accumulator with "" is a habit from string code. When the next lines call append, Python tells you the truth: the object is a str. Decide first what you are building. A collection of items is a list. A single piece of text is a string.
Cause 2: a function returns different types
A function that returns "none found" on one path and a list on another makes every caller guess. Return the same type on every path, an empty list for no result, and keep messages out of return values.
Cause 3: the data came in as text
Values read with input(), from a file, from environment variables or from a query string are strings. If you expect several values, split them:
python3 - <<'PY'
raw = "ann,bob,cy"
names = raw.split(",")
names.append("dee")
print(names)
PYOutput
['ann', 'bob', 'cy', 'dee']
Fix it
- Print
type(name)before the failing line, or check withisinstance(name, list). - Walk back to every assignment of that name and make them all produce a list, or rename the string variable.
- When you really are building text, collect parts in a list and join them at the end:
python3 - <<'PY'
parts = []
for word in ["a", "b", "c"]:
parts.append(word)
print("-".join(parts))
PYOutput
a-b-c
Run the code again to confirm the traceback is gone and the output is what you expect.
Prevent it
Use clear names (names for a list, name for text) and add type hints such as names: list[str] = []. A type checker like mypy or pyright reports "str" has no attribute "append" before you run the code. The sibling error 'NoneType' object has no attribute has the same shape with a different cause: a function that returned None.
FAQ
Do strings have an append method?
No. Strings are immutable, so they have no method that changes them in place. Build a new string with +, an f-string or str.join(), or collect pieces in a list and join them at the end.
Why does the same code work for a list but fail for a string?
append is a list method. The error means the object at that moment is a str, so look for an earlier assignment that replaced your list.
How do I see the type of the variable that failed?
Print type(variable) just before the failing line, or run the code in a debugger. The message already names the type in quotes: 'str' here, and 'dict_keys', 'int' or 'tuple' in the same error for other types.