The String isdigit() method returns True if the string is not empty and all the characters are digits. Otherwise, it returns False.
Syntax
my_string.isdigit()isdigit() Parameters
Python String isdigit() method does not take any parameters.
isdigit() Return Value
Python String isdigit() method returns:
Trueif ALL characters in the string are digits.Falseif AT LEAST ONE character is NOT a digit.
Examples
Example 1: Check if a String is digit with isdigit()
The isdigit() method returns True if all characters in the string are digits.
my_str = '123'
result = my_str.isdigit()
print(result) # Output: Trueoutput
TrueThe isdecimal() method returns False if at least one character is not a digit.
For example, string of a float point number is not digit:
my_str = '123.456'
result = my_str.isdigit()
print(result) # Output: Falseoutput
FalseFor example, string of number with thousands separator is not digit:
my_str = '1,234,567'
result = my_str.digit()
print(result) # Output: Falseoutput
FalseFor example, empty string is not digit:
my_str = ''
result = my_str.isdigit()
print(result) # Output: Falseoutput
FalseExample 2: Check if a String with Unicode Digit Characters is digit with isdecimal()
Unicode characters, like U+0660 (Arabic-Indic Digit Zero), subscripts and superscripts, are also considered as digits.
my_str1 = '\u0660'
my_str2 = '\u00B23455' # my_str2 = '²3455'
my_str3 = '\u20823455' # my_str2 = '₂3455'
print(my_str1.isdigit()) # Output: True
print(my_str2.isdigit()) # Output: True
print(my_str3.isdigit()) # Output: Trueoutput
True
True
TrueSpecial Unicode characters like circled digits ⑥ are also considered as digits.
my_str = '\u2465' # Special Unicode ⑥
print(my_str.isdigit()) # Output: Trueoutput
TrueBut not all Unicode characters are valid digits!
For example, Vulgar Fractions are not digits!
my_str = '\u00BD' # my_str2 = '½'
print(my_str.isdigit()) # Output: Falseoutput
Falseisdigit() method vs isdecimal() method vs isnumeric() method
The main difference between the isdigit(), isdecimal() and isnumeric() methods is that:
-
isdigit()method supports Decimals, Subscripts, Superscripts -
isdecimal()method supports only Decimal Number -
isnumeric()method supports Digits, Vulgar Fractions, Subscripts, Superscripts, Roman Numerals, Currency Numerators.
Example with '123' as String:
print('123'.isdecimal()) # Output True
print('123'.isdigit()) # Output True
print('123'.isnumeric()) # Output Trueoutput
True
True
TrueExample with Superscript Two '²' (that is '\u00b2') as String:
print('\u00b2'.isdecimal()) # Output False
print('\u00b2'.isdigit()) # Output True
print('\u00b2'.isnumeric()) # Output Trueoutput
False
True
TrueExample with Vulgar Two '⅓' (that is '\u2153') as String:
print('\u2153'.isdecimal()) # Output False
print('\u2153'.isdigit()) # Output False
print('\u2153'.isnumeric()) # Output Trueoutput
False
False
True