What is the String isdigit() method in Python?

String isdigit() method

The isdigit() method is a built-in method in python for string handling. This method checks if the given string contains just digits such as 0123456789.

Note: Exponents, like ², are also considered to be digits.

Syntax

string.isdigit()

Parameter

The method isdigit() takes no parameters.

Return value

The isdigit() method returns true if all characters in the string are digits. Otherwise, it returns false.

Code

The following code shows how to use the isdigit() method in python:

# Python code to check whether the String
# consist of digit only
x = '458235001'
print(x.isdigit())
x = '\u0030243abir302'
print(x.isdigit())
# x = '²3447'
# subscript is a digit
x = '\u00B23447'
print(x.isdigit())
# x = '½'
# A fraction is not digit
x = '\u00BD'
print(x.isdigit())

Note:

  • “\u0030” is Unicode for 0
  • “\u00B2” is Unicode for ²
  • ‘\u00BD’ is Unicode for ½

Free Resources