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

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

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

The Python "AttributeError 'tuple' object has no attribute 'strip'" occurs when we call the strip() method on a tuple instead of a string. To solve the error, correct the assignment of the variable or access the tuple at a specific index when calling strip().

attributeerror tuple object has no attribute strip

Here is an example of how the error occurs.

main.py
example = ' hello ', ' world ' print(type(example)) # 👉️ <class 'tuple'> # ⛔️ AttributeError: 'tuple' object has no attribute 'strip' result = example.strip()

We declared a tuple instead of a string by separating multiple values with a comma.

Tuples are very similar to lists, but implement fewer built-in methods and are immutable (cannot be changed).

To be able to use the strip() method, we have to declare a string, or access the tuple at a specific index.

main.py
example = ' hello world ' print(type(example)) # 👉️ <class 'string'> result = example.strip() print(result) # 👉️ "hello world"

We declared a string, so we were able to use the strip() method to remove any leading and trailing whitespace.

If you meant to call strip() on an element of a tuple, access it at the specific index.

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

We accessed the tuple element at index 1 which is a string, so we were able to call the strip() method and stored the result.

Tuples are constructed in multiple ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

Note that tuples are immutable, so if you have to mutate a sequence, you have to use a list instead.

There are only 2 methods that you will likely be using on tuple objects.

main.py
my_tuple = ('a', 'b', 'c', 'c') print(my_tuple.count('c')) # 👉️ 2 print(my_tuple.index('a')) # 👉️ 0

The count method returns the number of occurrences of the value in the tuple and the index method returns the index of the value in the tuple.

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

main.py
my_tuple = ('a', 'b', 'c', 'c') # 👇️ [... 'count', 'index' ...] print(dir(my_tuple))

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: tuple object has no attribute" error.

Conclusion #

The Python "AttributeError 'tuple' object has no attribute 'strip'" occurs when we call the strip() method on a tuple instead of a string. To solve the error, correct the assignment of the variable or access the tuple at a specific index when calling strip().

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.