Python String isdigit() Function
The String isdigit()
method returns True
if the string is not empty and all the characters are digits. Otherwise, it returns False
.
Unicode characters such as superscript digits (like ¹
, ²
, ³
) and subscripts digits (like ₁
, ₂
, ₃
) are also considered as digits.
Syntax
my_string.isdigit()
isdigit() Parameters
Python String isdigit()
function does not take any parameters.
isdigit() Return Value
Python String isdigit()
function returns:
True
if ALL characters in the string are digits.False
if 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: True
output
True
The 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: False
output
False
For example, string of number with thousands separator is not digit:
my_str = '1,234,567'
result = my_str.digit()
print(result) # Output: False
output
False
For example, empty string is not digit:
my_str = ''
result = my_str.isdigit()
print(result) # Output: False
output
False
Example 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: True
output
True
True
True
Special Unicode characters like circled digits ⑥ are also considered as digits.
my_str = '\u2465' # Special Unicode ⑥
print(my_str.isdigit()) # Output: True
output
True
But not all Unicode characters are valid digits!
For example, Vulgar Fractions are not digits!
my_str = '\u00BD' # my_str2 = '½'
print(my_str.isdigit()) # Output: False
output
False
isdigit() 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 True
output
True
True
True
Example with Superscript Two '²'
(that is '\u00b2'
) as String:
print('\u00b2'.isdecimal()) # Output False
print('\u00b2'.isdigit()) # Output True
print('\u00b2'.isnumeric()) # Output True
output
False
True
True
Example with Vulgar Two '⅓'
(that is '\u2153'
) as String:
print('\u2153'.isdecimal()) # Output False
print('\u2153'.isdigit()) # Output False
print('\u2153'.isnumeric()) # Output True
output
False
False
True