AttributeError: 'str' object has no attribute 'pop'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

AttributeError: 'str' object has no attribute 'pop' #

The "AttributeError: 'str' object has no attribute 'pop'" occurs when we try to call the pop() method on a string instead of a list. To solve the error, call the pop() method on a list or use the rsplit() method if you need to remove the last word from a string.

attributeerror str object has no attribute pop

Here is an example of how the error occurs.

main.py
my_str = 'one two three' # ⛔️ AttributeError: 'str' object has no attribute 'pop' print(my_str.pop())

We tried to call the pop() method on a string and got the error.

Make sure you are not accessing the list at a specific index before calling pop().

If you need to remove an item from a list, call the pop() method on the list.

main.py
my_list = ['one', 'two', 'three'] my_list.pop() print(my_list) # 👉️ ['one', 'two']

The list.pop method removes the item at the given position in the list and returns it.

If no index is specified, the pop() method removes and returns the last item in the list.

If you need to remove the last word from a string, use the rsplit() method.

main.py
my_str = 'one two three' result = my_str.rsplit(' ', 1)[0] print(result) # 👉️ "one two"

The str.rsplit method returns a list of the words in the string using the provided separator as the delimiter string.

The method takes the following 2 arguments:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done, the rightmost ones (optional)
main.py
my_str = 'one two three' print(my_str.rsplit(' ')) # 👉️ ['one', 'two', 'three'] print(my_str.rsplit(' ', 1)) # 👉️ ['one two', 'three']

Except for splitting from the right, rsplit() behaves like split().

To remove the last word from the string, we split the string on the last space with maxsplit set to 1.

Since rsplit splits from the right, we split only the last word and access the rest of the string at position 0 of the list.

A good way to start debugging is to print(dir(your_object)) and see what attributes a string has.

Here is an example of what printing the attributes of a string looks like.

main.py
my_string = 'hello world' # [ 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', # 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', # 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', # 'lstrip', 'maketrans', 'partition', 'removeprefix', 'removesuffix', 'replace', 'rfind', 'rindex', # 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', # 'title', 'translate', 'upper', 'zfill'] print(dir(my_string))

If you pass a class to the dir() function, it returns a list of names of the class's attributes, and recursively of the attributes of its bases.

If you try to access any attribute that is not in this list, you would get the "AttributeError: str object has no attribute error".

Since pop() is not a method implemented by strings, the error is caused.

Conclusion #

The "AttributeError: 'str' object has no attribute 'pop'" occurs when we try to call the pop() method on a string instead of a list. To solve the error, call the pop() method on a list or use the rsplit() method if you need to remove the last word from a string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.