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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError: 'str' object has no attribute 'remove'" occurs when we try to call the remove() method on a string instead of a list. To solve the error, call the remove() method on a list or use the replace() method if trying to remove characters from a string.

attributeerror str object has no attribute remove

Here is an example of how the error occurs.

main.py
my_list = ['a', 'b', 'c'] # ⛔️ AttributeError: 'str' object has no attribute 'remove' my_list[0].remove('a')

We accessed the list element at index 0 (a string) and called the remove() method on the string which caused the error.

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

main.py
my_list = ['a', 'b', 'c'] my_list.remove('a') print(my_list) # 👉️ ['b', 'c']

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

The method raises a ValueError if there is no such item.

If you need to handle a scenario where the item is not found in the list, use a try/except statement.

main.py
my_list = ['a', 'b', 'c'] try: my_list.remove('r') except ValueError: print('Item not in list') print(my_list) # 👉️ ['a', 'b', 'c']

The remove() method mutates the original list and returns None.

If you are trying to remove characters from a string, use the replace() method instead.

main.py
my_str = 'apple banana kiwi' new_str = my_str.replace('banana', '') print(new_str) # 👉️ "apple kiwi"

By replacing a substring with an empty string, we effectively remove the substring from the string.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)
Note that the method doesn't change the original string. Strings are immutable in Python.

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 the str object doesn't implement a remove() method, the error is caused.

Conclusion #

The Python "AttributeError: 'str' object has no attribute 'remove'" occurs when we try to call the remove() method on a string instead of a list. To solve the error, call the remove() method on a list or use the replace() method if trying to remove characters 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.