AttributeError: 'list' object has no attribute 'strip'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

AttributeError: 'list' object has no attribute 'strip' #

The Python "AttributeError: 'list' object has no attribute 'strip'" occurs when we call the strip() method on a list instead of a string. To solve the error, call strip() on a string, e.g. by accessing the list at a specific index or by iterating over the list.

attributeerror list object has no attribute strip

Here is an example of how the error occurs.

main.py
my_list = [' hello ', ' world '] # ⛔️ AttributeError: 'list' object has no attribute 'strip' my_list.strip()

We created a list with 2 elements and tried to call the strip() method on the list which caused the error.

The strip() method is string-specific, so we have to call it on a string, and not on a list object.

One way to solve the error is to access the list at a specific index before calling strip().

main.py
my_list = [' hello ', ' world '] result = my_list[0].strip() print(result) # 👉️ "hello"

We accessed the list element at index 0 and called the strip() method on the string.

If you need to call the strip() method on each string in a list, use a list comprehension.

main.py
my_list = [' hello ', ' world '] new_list = [word.strip() for word in my_list] print(new_list) # 👉️ ['hello', 'world']

Alternatively you can use a for loop to call the strip() method on each string in the list.

main.py
my_list = [' hello ', ' world '] for word in my_list: result = word.strip() print(result) # 👉️ "hello", "world"

We used a for loop to iterate over the list and called the strip() method on each string.

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

The method does not change the original string, it returns a new string. Strings are immutable in Python.

The "AttributeError: 'list' object has no attribute 'strip'" occurs when we try to call the strip() method on a list instead of a string.

To solve the error, you either have to correct the assignment of the variable and make sure to call strip() on a string, or call strip() on an element in the list that is of type string.

You can either access the list at a specific index, e.g. my_list[0] or use a for loop to iterate over the list if you have to call strip() on each element.

You can view all the attributes an object has by using the dir() function.

main.py
my_list = ['a', 'b', 'c'] # 👉️ [... 'append', 'clear', 'copy', 'count', 'extend', 'index', # 'insert', 'pop', 'remove', 'reverse', 'sort' ...] print(dir(my_list))

If you pass a class to the dir() function, it returns a list of names of the classes' 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: list object has no attribute" error.

Since strip() is not a method implemented by lists, the error is caused.

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.