Python String isdecimal() Methods

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

Decimal characters are those that can be used to form numbers in base 10. All the Unicode General Category 'Nd' are considered as decimal characters.

Syntax:

str.isdecimal()

Parametrs:

None.

Return Value:

Returns True if all characters in the string are decimals; otherwise returns False even if one character is not decimal.

The following examples demonstrate isdecimal() method.

Example: isdecimal()
numstr = '12345'
print(numstr.isdecimal()) # returns True

numstr = '10.50'
print(numstr.isdecimal()) # returns False

alnumstr = '123A'
print(alnumstr.isdecimal()) # returns False

mystr = 'Python'
print(mystr.isdecimal()) # returns False
Output
True
False
False
False

The isdecimal() method will return False if a string contains whitespaces, symbols or alphabets.

Example: Symbols with isdecimal()
amount = '$100'
print(amount.isdecimal())

code = '#007'
print(mystr.isdecimal())
Output
False
False

All the characters under Unicode General Category 'Nd' are decimals e.g. isdecimal() returns True for the Arabic decimal two is ٢ decimal.

Example: Unicode Decimal Chars with isdecimal()
arabicdecimal = '٢'  # 2 in Arabic
print(arabicdecimal.isdecimal())
Output
True

Superscript and subscripts (usually written using unicode) are considered digit characters and not decimal. Hence, if the string contains these characters along with decimal characters, isdecimal() returns False.

Example:
mystr = '\u00B23455'     # returns subscript 2 with the number 3455
print(mystr.isdecimal())

mystr = '\u2156' # Unicode chars of ⅖
print(mystr.isdecimal())

mystr = '\u0032' # Unicode chars of 2
print(mystr.isdecimal())
Output
False
False
True

The following table lists difference among the isdecimal(), isdigit(), and isnumeric() methods based on the given inputs:

Input String Value isdecimal() isdigit() isnumeric()
'123' True True True
'$123' False False False
'123.50' False False False
'123a' False False False
'¾' False False True
'\u0034' True True True
Want to check how much you know Python?