Skip to content
Tutorials Center
My learning

Topics

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.

Python
print("2025".isdecimal())   # True
print("12.5".isdecimal())   # False

Try it

Python What counts as decimal

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.

Python Validating before int()

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

Python
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() is False, 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 all False, because -, . and a space are not digits. Strip whitespace first if you want to allow it.
  • Not just ASCII. "٣٤٥".isdecimal() is True, and int("٣٤٥") returns 345. If you only want 0 to 9, add an ASCII check such as text.isascii() and text.isdecimal().
  • Stricter than isdigit(). Superscripts like "²" are digits but not decimals, so "²".isdecimal() is False while "²".isdigit() is True. Passing such a string to int() would fail, which is why isdecimal() 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, catching ValueError from int() is simpler and more complete than testing first.
  • Works on str only. bytes has its own isdigit() for ASCII digits but no isdecimal().