Python isdecimal() Method

Definition
In Python, isdecimal() returns True when every character is a decimal digit and the string is not empty.
It takes no arguments. Signs, dots, letters, and spaces make it return False. It is stricter than isdigit().
Syntax
Call isdecimal on the string. Start with this line.
| Call | What it does |
|---|
"42".isdecimal() | Returns True. |
"3.14".isdecimal() | Returns False (dot is not a digit). |
isdecimal() checks characters. It does not parse floats or signed numbers.
Library declarations
Parameters
This method has no parameters.
Return Value
| Return Value | Description |
|---|
| bool | True if all characters are decimal digits. |
Python Examples
Example 1 — Basic isdecimal() Checks
See what passes and what fails—including the empty string.
print("123".isdecimal())
print("12.3".isdecimal())
print("-5".isdecimal())
print("".isdecimal())
How It Works
- All digits.
"123" has only 0–9 → True. - Dot fails.
"." is not a decimal digit → False. - Sign fails.
"-" is not a decimal digit → False. - Empty fails.
"" is never decimal → False.
Example 2 — Validate Clean Input
Strip spaces, then accept only pure digit strings before converting.
raw = " 42 "
text = raw.strip()
if text.isdecimal():
number = int(text)
print("OK:", number)
else:
print("Not a whole number")
How It Works
- Clean first.
strip() removes leading/trailing spaces. - Gate with isdecimal. Only digit-only text continues.
- Safe convert.
int(text) is safe after a True check for ASCII digits. - Reject junk. Signs, dots, or letters would take the
else branch.
Example 3 — isdecimal() vs isdigit()
Superscript digits pass isdigit() but fail isdecimal().
s = "²" # superscript two
print("isdecimal:", s.isdecimal())
print("isdigit: ", s.isdigit())
isdecimal: False
isdigit: True
How It Works
- Same character. Both methods inspect
"²". - isdecimal(). Superscripts are not decimal digits →
False. - isdigit(). Accepts some extra digit forms →
True. - Pick by goal. Strict base-10 digits →
isdecimal; broader digit check → isdigit.
Frequently Asked Questions
It returns True if every character is a decimal digit and the string is not empty.
No. "3.14" and "-5" both return False.
isdecimal() is stricter. isdigit() can accept some characters such as superscripts.
It returns False.
No. It only returns a boolean.
Next: isdigit()
Learn the broader digit check and how it differs from isdecimal().
isdigit() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful