Select Page

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

by | Programming, Python, Tips

In Python, isdigit() is a string method that checks if a string holds a numerical value. You cannot call the isdigit() method on an integer, and if you do, you will raise the AttributeError ‘int’ object has no attribute ‘isdigit’.

This error commonly occurs if you use the eval() method on a numerical string, which will return a number.

To solve this error, ensure you do not call the eval() method on a string before calling isdigit().

This tutorial will go through the error and how to solve it with code examples.


AttributeError: ‘int’ object has no attribute ‘isdigit’

AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. The part “‘int’ object has no attribute ‘isdigit’” tells us that the integer object we are handling does not have isdigit() as an attribute.

The isdigit() method belongs to the string data type and checks if all characters in the string are digits. The method returns True if all the characters are digits, otherwise False.

Let’s look at an example of calling the isdigit method on a string with all digits:

txt = "90059"

x = txt.isdigit()

print(x)
True

isdigit() evaluates to True because the string contains all digits.

Let’s see what happens when we call isdigit() on a string with some digits:

txt = "h3ll0 w0r1d"

x = txt.isdigit()

print(x)
False

isdigit() evaluates to False because the string contains some numbers and some alphabetical characters.

Now let’s see what happens when we try to call isdigit() on an integer:

number = 10

x = number.isdigit()

print(x)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-34-193157514d01> in <module>
      1 number = 10
      2 
----> 3 x = number.isdigit()
      4 
      5 print(x)

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

We get the AttributeError because the isdigit() is not an integer method. It is a string method.

Example

You may encounter this error when using the eval() method on a string. The eval() method evaluates the specified expression, and if it is a legal Python statement, it will execute it. If we pass a numerical string to the eval function, it will return an integer.

Let’s write a program that calculates the interest applied to a deposit after a year. We will use the input() function to take input from the user:

# Define interest value

interest = 1.05

# Get Input from user

deposit = eval(input("Enter the deposit amount for the year: "))

print(f'initial deposit {deposit}')

# Check if deposit is a digit

if deposit.isdigit():

# Calculate value after interest 

    deposit *= interest

# Print result

    print(f'Deposit after 1 year {round(deposit,0)}')

else:

# If not digit print incorrect input 

    print("Incorrect input")

Let’s run the code to see the result:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-39-c4eff49debd2> in <module>
      2 deposit = eval(input("Enter the deposit amount for the year: "))
      3 print(f'initial deposit {deposit}')
----> 4 if deposit.isdigit():
      5     deposit *= interest
      6     print(f'Deposit after 1 year {deposit}')

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

The error occurs because the eval() method evaluates the input to an integer.

Solution

To solve this error, we can remove the eval() method. The input() function returns a string by default. Then we can check if the value is a numerical value using isdigit(), and if so, we convert it to a float then calculate the interest. Let’s look at the revised code:

# Define interest

interest = 1.05

# Get input from user

deposit = input("Enter the deposit amount for the year: ")

print(f'initial deposit {deposit}')

# Check if input is a digit

if deposit.isdigit():

# Convert string to float value

    deposit = float(deposit)

# Calculate value after interesst

    deposit *= interest

# Print result

    print(f'Deposit after 1 year {round(deposit,0)}')

else:

# If not digit print incorrect input 

    print("Incorrect input")
Enter the deposit amount for the year: 
3000
Deposit after 1 year 3150.0

We successfully check if the input is a numeric value and calculate the annual interest.

Summary

Congratulations on reading to the end of this tutorial! The error AttributeError: ‘int’ object has no attribute ‘isdigit’ occurs when you try to call the isdigit() method on an integer instead of a string. You may encounter this error if you call the eval() method on a numerical string, which returns an integer value. To solve this error, remove eval() and call isdigit() directly on the string.

Generally, check the type of object before calling the isdigit() method.

For further reading on AttributeErrors involving the list object, go to the articles:

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!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!