Last modified: Dec 12, 2025 By Alexander Williams
Fix Python AttributeError 'int' object has no attribute 'strip'
Python errors can be confusing. A common one is AttributeError. This error means you tried to use a method on the wrong data type.
The message "'int' object has no attribute 'strip'" is very specific. It tells you the core problem. You are trying to call strip() on an integer.
Understanding the Error
The strip() method is for strings. It removes whitespace from the start and end. Integers (int) are numbers. They do not have this method.
Python is a dynamically typed language. This means a variable's type is set at runtime. You might think a variable is a string. But it could be an integer.
This mismatch causes the AttributeError. The integer object does not have a 'strip' attribute. Python stops and shows you the error.
Common Causes and Examples
Let's look at typical scenarios. These often cause the 'int' object 'strip' error.
1. User Input Conversion
You get input from a user with input(). Then you try to strip it. But you converted it to an int first.
# This will cause an error
user_value = input("Enter a number: ") # Returns a string
number = int(user_value) # Convert to integer
cleaned = number.strip() # ERROR: Can't strip an int!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'strip'
The fix is simple. Strip the string before converting it to an integer.
# Correct order: strip first, then convert
user_value = input("Enter a number: ")
cleaned_string = user_value.strip() # Strip the string
number = int(cleaned_string) # Now convert to int
print(f"Number is: {number}")
2. Data from Mixed Sources
You might read data from a file or API. Some values are strings. Others are already integers. Your code assumes they are all strings.
data_list = [" 42 ", 100, " hello "]
for item in data_list:
print(item.strip()) # Fails on the integer 100
You need to check the type first. Or convert everything to a string.
data_list = [" 42 ", 100, " hello "]
for item in data_list:
# Convert to string first, then strip
cleaned = str(item).strip()
print(cleaned)
3. Dictionary or JSON Data
JSON keys and values can be mixed types. You might access a value thinking it's a string. But it's stored as a number.
import json
json_data = '{"name": "Alice", "id": 12345}'
data = json.loads(json_data)
user_id = data["id"]
cleaned_id = user_id.strip() # ERROR: id is an int
Always verify the data type. Use type() or isinstance().
user_id = data["id"]
if isinstance(user_id, str):
cleaned_id = user_id.strip()
else:
# It's already an int, or convert it
cleaned_id = str(user_id).strip()
print(cleaned_id)
General Debugging Strategy
Follow these steps to find and fix the error.
Step 1: Identify the Variable
The error traceback shows the line number. Find which variable is the integer.
Step 2: Check Its Type
Use print(type(variable)) before the error line. Confirm it's an int.
Step 3: Find Why It's an Integer
Trace back in your code. Did it come from user input? A calculation? A database?
Step 4: Apply the Fix
Choose the right solution. Convert to string. Or change the logic order.
Best Practices to Avoid the Error
Prevention is better than fixing. Adopt these habits.
Validate and Clean Data Early
Clean strings at the point of entry. Convert types right after input.
Use Type Checking
Use isinstance() in functions that expect specific types. It makes code robust.
def clean_input(value):
if isinstance(value, (int, float)):
value = str(value)
# Now it's safe to strip
return value.strip()
Write Defensive Code
Assume data might be the wrong type. Use try-except blocks to handle errors gracefully.
try:
cleaned = my_var.strip()
except AttributeError:
# If it fails, convert to string and try again
cleaned = str(my_var).strip()
Related AttributeErrors
This error is part of a family. Confusing methods for data types is common.
You might see 'list' object has no attribute 'strip'. Lists also lack string methods. The fix is similar. Check our guide on Fix Python AttributeError 'list' object has no attribute 'strip'.
Another is 'dict' object has no attribute 'lower'. Dictionaries don't have the lower() method. Learn more in the article Fix Python AttributeError 'dict' object has no attribute 'lower'.
Also common is 'int' object has no attribute 'split'. Like strip(), split() is for strings. See Fix Python AttributeError: 'int' Object Has No Attribute 'split' for help.
Conclusion
The AttributeError for 'int' and 'strip' is a type error. You are using a string method on a number.
The fix is straightforward. Ensure you are working with a string. Convert the integer using str() before calling strip().
Better yet, understand your data flow. Validate types early. Write defensive code.
This approach solves not just this error. It makes your Python code stronger and more reliable.