Home › Errors › AttributeError
AttributeError: 'str' object has no attribute 'append'
Exception: AttributeErrorCategory: runtime (attribute lookup)Raised at: the .append( or similar list method called on a string
What this error means
You called a list method on a string. append belongs to list; strings do not have it, and never will, because strings are immutable — they cannot be changed in place at all.
Traceback (most recent call last):
File "demo.py", line 4, in <module>
result.append("x")
AttributeError: 'str' object has no attribute 'append'
The message is precise, which makes it one of the easier errors to diagnose: 'str' object tells you the type of the thing on the left of the dot, and has no attribute 'append' tells you what is missing. The name you used simply is not a method of that type.
Why strings have no append
Immutability is not an arbitrary restriction — it is the reason strings can be safely shared, hashed and used as dictionary keys. If a string could be modified in place, every other reference to it would change underneath you.
s = "abc"
s[0] = "z" # 💥 TypeError: 'str' object does not support item assignment
s.append("d") # 💥 AttributeError: 'str' object has no attribute 'append'
s += "d" # ✅ but this REBINDS s to a new string; the old one is untouched
Note what += really does. It does not modify the original string. It builds a brand-new string and points the name at it, leaving the old object alone. That is why the same operation on a list (lst += [x]) behaves differently — lists are mutable and extend in place.
The correct way to build a string
Collect the pieces in a list, then combine them once with join. This is both correct and much faster than repeated concatenation:
result = []
for word in words:
result.append(word.upper())
text = ", ".join(result) # ✅
# or as a comprehension
text = ", ".join(w.upper() for w in words) # ✅
# the wrong shape this replaces
result = ""
for word in words:
result += word.upper() + ", " # ⚠️ works, but O(n²) and leaves a trailing ", "
result = result.rstrip(", ")
The performance difference is real, not theoretical. Because strings are immutable, every += copies the entire accumulated string. For a few items nobody notices; for tens of thousands it becomes the slowest line in your program. join sizes the output once and fills it in a single pass.
Other list methods missing on strings
If you reached for append, you may reach for these next. None of them exist on str, and each has a string equivalent:
lst.append(x) # no str equivalent — use += or join
lst.extend(xs) # no str equivalent — use += or join
lst.insert(i, x) # s = s[:i] + x + s[i:]
lst.remove(x) # s = s.replace(x, "", 1)
lst.pop() # s = s[:-1]
lst.sort() # s = "".join(sorted(s))
lst.reverse() # s = s[::-1]
lst.clear() # s = ""
len(lst), lst[0] # ✅ these DO work on both — len and indexing are shared
The last line is the part worth remembering: len(), indexing, slicing, in, iteration and + all work on both lists and strings. It is only the mutating methods that are list-only, and that is a direct consequence of immutability.
The reverse mistake
The same confusion runs the other way, and it produces a different error. Calling a string method on a list:
items = ["a", "b"]
items.upper() # 💥 AttributeError: 'list' object has no attribute 'upper'
items.strip() # 💥 same cause
items.split(",") # 💥 same cause
" ".join(items).upper() # ✅ convert first, then use string methods
And a third variant, which is subtler: iterating a string gives you single characters, so calling a string method on the loop variable is right, but treating it as a whole word is not:
for ch in "abc":
ch.upper() # ✅ ch is a one-character string
ch.append("x") # 💥 still no append — ch is a str, not a list
Related messages worth knowing
- AttributeError: the parent guide — the general shape of this error and how to read the two quoted parts.
- TypeError: 'str' object does not support item assignment — the same immutability, hit through [] instead of a method.
- AttributeError: module has no attribute — the import-shaped version of the same message.
🇮🇳 Hindi में समझें
AttributeError: 'str' object has no attribute 'append' ka matlab hai aapne ek string par list ka method call kar diya. append sirf list ka method hai — strings par ye nahi hai aur kabhi nahi hoga, kyunki strings immutable hain, matlab unhe in-place badla hi nahi ja sakta. Ye restriction manmaani nahi hai: isi wajah se strings safely share ki ja sakti hain, hash ho sakti hain, aur dictionary key ban sakti hain. s[0] = 'z' bhi isi wajah se fail hota hai. s += 'd' kaam karta hai, lekin wo original string ko badalta nahi — wo ek nayi string banakar naam ko us par point kar deta hai. Isliye list par lst += [x] alag behave karta hai, kyunki lists mutable hain. Sahi tareeka: pieces ko ek list me collect karo, phir ek baar join se jodo — text = ', '.join(parts). Ye sirf sahi hi nahi, bahut tez bhi hai, kyunki har += poori accumulated string ko copy karta hai (O(n²)), jabki join ek hi pass me output bhar deta hai. Yaad rakhne wali baat: len(), indexing, slicing, in, iteration aur + dono par kaam karte hain — sirf mutating methods list-only hain. Aur ye galti ulta bhi hoti hai: items.upper() par 'list' object has no attribute 'upper' milega.