If you are in trouble with the error “AttributeError: ‘str’ object has no attribute ‘pop'” in Python, read this article carefully. We are about to show you the root of the problem and give you a few methods to deal with the error. Let’s move on.
The reason for AttributeError: ‘str’ object has no attribute ‘pop’ in Python
The AttributeError: ‘str’ object has no attribute ‘pop’ error is raised when you try to call the pop()
method on a string object in Python. The pop()
method is not a valid method for strings, as it is only available for list objects.
Here is an example of code that would raise this error:
Code:
text = "Welcome to Learn Share IT" text.pop() # Error raise in this line
Result:
AttributeError Traceback (most recent call last)
in <module>
----> 2 text.pop() # Error raise in this line
AttributeError: 'str' object has no attribute 'pop'
Solution to AttributeError: ‘str’ object has no attribute ‘pop’
To fix this error, you can use a list object instead of a string or a different method available for strings, such as slice()
or replace()
.
Convert the string into a list
You must first transform the string into a list to call the pop()
attribute without error. To make the string become a list, use the split()
function with the delimiter as the single white space. For example:
Code:
text = "Welcome to Learn Share IT" print("The original string is:", text) # Split the string text = text.split(" ") # Pop the element at the index 3 text.pop(3) # Merge the string text = " ".join(text) print("The new string is:", text)
Result:
The original string is: Welcome to Learn Share IT
The new string is: Welcome to Learn IT
Slice the string
The “slice” technique returns a substring from the original string based on the defined interval in the function. For example, if you want to remove the last character from a string.
Code:
text = "Welcome to Learn Share IT" print("The original string is:", text) # Remove the last character text = text[:-1] print("The new string is:", text)
Result:
The original string is: Welcome to Learn Share IT
The new string is: Welcome to Learn Share I
Use the replace() method
Alternatively, you can use the replace()
method to replace the last character with an empty string:
Code:
text = "Welcome to Learn Share IT" print("The original string is:", text) # Replace the last character with an empty character text = text.replace(text[-1], "") print("The new string is:", text)
Result:
The original string is: Welcome to Learn Share IT
The new string is: Welcome to Learn Share I
Summary
In summary, the error “AttributeError: ‘str’ object has no attribute ‘pop'” occurs when you apply the pop()
attribute to a string, which is unavailable to the string type. To get rid of the error, you can create a list from the string and then apply the pop()
attribute; or use the slice technique or the replace()
method to remove the target characters.

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP