How to Solve Python AttributeError: ‘int’ object has no attribute ‘strip’

by | Programming, Python, Tips

Introduction

When programming in Python, especially when working with user input or reading data from external sources, you might encounter the following error:

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

This error occurs when you mistakenly try to use the strip() method, which is available for strings, on an integer object. The strip() method is used to remove leading and trailing whitespace from a string, but integers do not have this method, leading to the AttributeError.

Example: Using input()

A common situation in which this error occurs is when working with the input() function. In Python, input() always returns data as a string, even if the input looks like an integer. However, if you convert this input to an integer and later try to apply the strip() method to it, you’ll run into this error. Let’s go through an example:

user_input = input("Enter a number: ")
user_input = int(user_input)  # Converting input to an integer
cleaned_input = user_input.strip()  # AttributeError: 'int' object has no attribute 'strip'

In this case, after converting user_input to an integer using int(), the variable no longer supports string methods like strip(). This causes the AttributeError.

Solution

To avoid this error, apply the strip() method before converting the input to an integer. Here’s the corrected code:

user_input = input("Enter a number: ")
cleaned_input = user_input.strip()  # First, strip the input to remove any spaces
cleaned_input = int(cleaned_input)  # Then, convert the cleaned string to an integer
print(cleaned_input)

In this updated version, we apply strip() to the input while it’s still a string and then convert the stripped value to an integer. This avoids the AttributeError while cleaning up any unwanted spaces from the input.

Example: Handling Mixed Data

This error is also common when processing a list with mixed data types, such as integers and strings. For example, if you have a list containing both integers and strings, and you try to apply the strip() method to every item, an AttributeError will occur:

data = [123, '  456 ', ' hello ', 789]
for item in data:
    cleaned_item = item.strip()  # AttributeError: 'int' object has no attribute 'strip'

In this example, the loop tries to apply strip() to both strings and integers, but strip() only works on strings, leading to an error when encountering an integer like 123 or 789.

Solution

To handle mixed data types, you can use Python’s isinstance() function to apply the strip() method only to strings, and leave the integers unchanged:

data = [123, '  456 ', ' hello ', 789]
for item in data:
    if isinstance(item, str):
        cleaned_item = item.strip()
    else:
        cleaned_item = item  # Leave integers unchanged
    print(cleaned_item)

Output:

123
456
hello
789

In this solution, strip() is applied only to strings, and the integers are passed through without modification, preventing the AttributeError.

Summary

  • The strip() method is for strings, not integers.
  • When using input(), apply strip() before converting the input to an integer.
  • Use isinstance() to check the type of data when working with mixed inputs to prevent applying string methods to non-string objects.

Congratulations on reading to the end of this tutorial!

For further reading on 'int' AttributeErrors, go to the article:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee