The String isdecimal() method returns True if the string is not empty and all the characters are decimal characters. Otherwise, it returns False.
Syntax
my_string.isdecimal()isdecimal() Parameters
Python String isdecimal() method does not take any parameters.
isdecimal() Return Value
Python String isdecimal() method 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: Trueoutput
TrueThe 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: Falseoutput
FalseFor example, string of number with thousands separator is not decimal:
my_str = '1,234,567'
result = my_str.isdecimal()
print(result) # Output: Falseoutput
FalseFor example, empty string is not decimal:
my_str = ''
result = my_str.isdecimal()
print(result) # Output: Falseoutput
FalseExample 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: Trueoutput
TrueBut 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: Falseoutput
False
Falseisdecimal() 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 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