Latest Code Tutorials

Python String isdigit() Function Example

Python string isdigit() is an inbuilt method that is used to check whether the given string consists of only digits. In other words, we can say that it checks if the characters present in the string are digits or not. Python isdigit() method returns true if the string has its all character as digits and false otherwise.

If you want to put the validation, then this function might be useful to you.

Exponents, like ², are also considered to be a digit.

Python String isdigit()

The isdigit() function is used to check if the argument contains digits such as 0123456789



The function doesn’t take any parameters. There will error shown if we try to pass any parameter to the method.

It returns true if the string consists of only digits.

It returns false if the string doesn’t contain digits or contains characters other than digits like numerical or special characters. When it identifies a space, then also it returns false.

We can use the ascii values of characters and print the digits using isdigit().

See the following example.

string_variable = “1234”



Write a program to show the mechanism of the isdigit() method.


string = "12345"
print("It will print true as the string contains only digits:")


It will print true as the string contains only digits:

Write a program to traverse the string using ascii value and print the digits.

y = ""
for var in range(0, 60, 1):
    x = chr(var)
    if(x.isdigit() == True):
        y += x

print("String:", y)


String: 0123456789

Python isdigit float

Python str.isdigit() will only return True if all characters in the string are digits. The ‘.’ is punctuation, not a digit. Formally, the digit is a character that has the property value Numeric_Type = Digit or Numeric_Type = Decimal.

(For Python 2, for str objects only the ASCII digits (0 through to 9) are considered, but for Unicode objects, the same definition applies).

Python isdigit() function with negative arguments

See the following code.


data = '-21'

if data.isdigit() is True:
    print('We can use isdigit() function on negative number')
    print('We can\'t use the isdigit() function')


We can't use the isdigit() function

I can type in numbers fine, and this would filter out alpha/alphanumeric strings.

When I put a negative number such as -1, ‘.isdigit’ counts ‘-‘ sign as a string value, and it rejects it. So, it would go to (else).

When I try ‘s1’ and ‘s’ for example, it will go to (else).

Errors And Exceptions

  1. It does not take any arguments; therefore, the isdigit() function returns an error if the parameter is passed.
  2. Superscript and subscripts are considered the digit characters along with decimal characters; therefore, Python isdigit() function returns “True”.
  3. The roman numerals, currency numerators, and fractions are not considered to be digits. Therefore, Python isdigit() function returns “False”.


  1. Initialize the new string and a variable count=0.
  2. It traverses every character using ASCII value, check if the character is the digit or not.
  3. If it is the digit, increment the count by 1 and add it to the new string, else traverse to the next character.
  4. Print the value of a counter and a new string.

Application of isdigit()

Using ASCII values of characters, count and print all the digits using the isdigit() function.

Finally, Python String isdigit() Function Example is over.

Related Posts

How to convert Python string to list

Python string capitalize()

Python string casefold()

Python string expandtabs()

Python string center()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.