Python Reference · String Methods
Python str.isdecimal() method
Reference for Python str.isdecimal(): test whether every character is a decimal digit, how it differs from isdigit() and isnumeric(), and safe int() checks.
The isdecimal() method returns True if the string is not empty and every character is a decimal digit, meaning a character that can form a base-10 number. That includes the ASCII digits 0 to 9 and the digits of other writing systems, such as Arabic-Indic or Devanagari digits.
It is the strictest of the three digit tests (isdecimal(), isdigit(), isnumeric()) and the one that matches what int() accepts as digits.
print("2025".isdecimal()) # True print("12.5".isdecimal()) # False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
str.isdecimal()
This method takes no parameters.
Return value: True if the string has at least one character and all characters have the Unicode category "Nd" (decimal number); otherwise False.
Raises: nothing.
Usage notes
- The empty string is
False."".isdecimal()isFalse, which is helpful for validation but surprising if you expect "all of nothing" to be true. - No signs, dots or spaces.
"-7","3.14"and"4 2"are allFalse, because-,.and a space are not digits. Strip whitespace first if you want to allow it. - Not just ASCII.
"٣٤٥".isdecimal()isTrue, andint("٣٤٥")returns345. If you only want0to9, add an ASCII check such astext.isascii() and text.isdecimal(). - Stricter than
isdigit(). Superscripts like"²"are digits but not decimals, so"²".isdecimal()isFalsewhile"²".isdigit()isTrue. Passing such a string toint()would fail, which is whyisdecimal()is the safer pre-check. - Stricter than
isnumeric(). Fractions ("½"), Roman numeral characters ("Ⅳ") and CJK numerals are numeric but not decimal. - A check is not a parser. Very long digit strings can still hit the integer-string conversion length limit in
int(). When the input is untrusted, catchingValueErrorfromint()is simpler and more complete than testing first. - Works on
stronly.byteshas its ownisdigit()for ASCII digits but noisdecimal().