Python String isdecimal() Function
The String isdecimal() method returns True if the string is not empty and all the characters are decimal characters. Otherwise, it returns False.
Decimal characters are those that can be used to form numbers in base 10 [0-9].
Unicode decimal character such as U+0660 (Arabic-Indic Digit Zero) is also considered as a decimal.
Syntax
my_string.isdecimal()
isdecimal() Parameters
Python String isdecimal() function does not take any parameters.
isdecimal() Return Value
Python String isdecimal() function returns:
Trueif ALL characters in the string are decimal characters.Falseif AT LEAST ONE character is NOT decimal.
Examples
Example 1: Check if a String is decimal with isdecimal()
The isdecimal() method returns True if all characters in the string are decimal characters.
my_str = '123'
result = my_str.isdecimal()
print(result) # Output: True
output
True
The isdecimal() method returns False if at least one character is not decimal.
For example, string of a float point number is not decimal:
my_str = '123.456'
result = my_str.isdecimal()
print(result) # Output: False
output
False
For example, string of number with thousands separator is not decimal:
my_str = '1,234,567'
result = my_str.isdecimal()
print(result) # Output: False
output
False
For example, empty string is not decimal:
my_str = ''
result = my_str.isdecimal()
print(result) # Output: False
output
False
Example 2: Check if a String with Unicode Decimal Characters is decimal with isdecimal()
Unicode characters, like U+0660 (Arabic-Indic Digit Zero), are also considered as decimal characters.
my_str = '\u0660'
result = my_str.isdigit()
print(result) # Output: True
output
True
But not all Unicode characters are decimal characters!
my_str1 = '\u00B23455' # my_str1 = '²3455'
my_str2 = '\u00BD' # my_str2 = '½'
print(my_str1.isdecimal()) # Output: False
print(my_str2.isdecimal()) # Output: False
output
False
False
isdecimal() method vs isdigit() method vs isnumeric() method
The main difference between the isdecimal(), isdigit() and isnumeric() methods is that:
-
isdecimal()method supports only Decimal Number -
isdigit()method supports Decimals, Subscripts, Superscripts -
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