The append() method adds items to the end of a list. It cannot be used to add items to a string. Python returns an error stating “AttributeError: ‘str’ object has no attribute ‘append’” if you try to add values to the end of a string using
In this guide, we talk about what this error means and why it is raised. We walk through an example of this error in action to help you learn how to fix it.
AttributeError: ‘str’ object has no attribute ‘append’
Python has a special function for adding items to the end of a string: concatenation.
To concatenate a string with another string, you use the concatenation operator (+). You use string formatting methods like f strings or
.format() if you want a value to appear inside another string at a particular point.
append() method does not work if you want to add a string to another string because
append() is only supported by list items.
The “AttributeError: ‘str’ object has no attribute ‘append’” error is raised when developers use
append() instead of the concatenation operator. It is also raised if you forget to add a value to a string instead of a list.
An Example Scenario
Write a program that makes a string containing the names of all the students in a class that begin with “S”. We start by defining a list that contains each student’s name and an empty string to which we will add the names that begin with “S”.
names = ["Sam", "Sally", "Adam", "Paulina"] s_names = ""
Next, we write a for loop that goes through this list of names. The code in our for loop will checks whether each name starts with the letter “S”:
for n in names: if n.startswith("S"): s_names.append(n) print("The students whose names begin with 'S' are: " + s_names)
Our for loop iterates over each name in the “names” list. In each iteration, our code checks if a name starts with the letter “S”. We do this using the startswith() method.
If a students’ name starts with “S”, we add it to the end of “s_names” using the
append() method. Once all of the names have been iterated over, our program prints out a message informing us of the students whose names begin with “S”.
Run our code and see what happens:
Traceback (most recent call last): File "main.py", line 6, in <module> s_names.append(n) AttributeError: 'str' object has no attribute 'append'
Our code fails to execute.
We’ve tried to use
append() to add each name to the end of our “s_names” string.
append() is not supported on strings which is why an error is returned.
We can fix our code by using the concatenation operator to add a name to the end of the “s_names” string. Let’s update our code to use the concatenation operator instead of
for n in names: if n.startswith("S"): s_names = s_names + n + " "
This time, we use the assignment operator to change the value of “s_names”. We use plus signs to create a string with the current value of “s_names”, followed by the name our loop is iterating over, followed by a blank space.
Run our code again to see if our fix works:
The students whose names begin with 'S' are: Sam Sally
Our code successfully identifies the names that begin with S. These names appear at the end of the string printed to the console. Each name is followed by a space which we specified when we used the concatenation operator in our for loop.
You encounter the “AttributeError: ‘str’ object has no attribute ‘append’” error if you try to use the
append() method to add an item to the end of a string.
append() method lets you add items to the end of a list, it cannot be used to add items onto a string. To solve this error, use the concatenation operator (+) to add items to the end of a string.
Now you’re ready to solve this common Python error like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.