Python String isdigit()

The isdigit() method returns True if all characters in a string are digits. If not, it returns False.

Example

str1 = '342'
print(str1.isdigit())
str2 = 'python'
print(str2.isdigit())
# Output: True # False

Syntax of String isdigit()

The syntax of isdigit() is

string.isdigit()

isdigit() Parameters

The isdigit() doesn't take any parameters.


Return Value from isdigit()

The isdigit() returns:

  • True if all characters in the string are digits.
  • False if at least one character is not a digit.

Example 1: Working of isdigit()

s = "28212"
print(s.isdigit())
# contains alphabets and spaces s = "Mo3 nicaG el l22er"
print(s.isdigit())

Output

True
False

A digit is a character that has property value:

  • Numeric_Type = Digit
  • Numeric_Type = Decimal

In Python, superscript and subscripts (usually written using unicode) are also considered digit characters. Hence, if the string contains these characters along with decimal characters, isdigit() returns True.

The roman numerals, currency numerators and fractions (usually written using unicode) are considered numeric characters but not digits. The isdigit() returns False if the string contains these characters.

To check whether a character is a numeric character or not, you can use isnumeric() method.


Example 2: String Containing digits and Numeric Characters

s = '23455'
print(s.isdigit())
#s = '²3455' # subscript is a digit s = '\u00B23455'
print(s.isdigit())
# s = '½' # fraction is not a digit s = '\u00BD'
print(s.isdigit())

Output

True
True
False

Also Read:

Did you find this article helpful?