Python isdecimal() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Methods

What You’ll Learn

The isdecimal() method returns True when every character in a string is a decimal digit and the string is not empty. It is ideal for validating whole-number input like PIN codes, ages, and quantities before you convert strings to integers with int().

01

Decimal Digits

0–9 and Unicode Nd.

02

Returns bool

True or False result.

03

No Arguments

Call directly on str.

04

Empty = False

Zero-length fails check.

05

Input Validation

PINs and numeric fields.

06

vs isdigit()

Stricter digit rules.

Definition and Usage

In Python, isdecimal() inspects each character in a string. If every character is a decimal digit (characters in Unicode category Nd that can form base-10 numbers) and the string has at least one character, it returns True. Otherwise it returns False. For example, "12345".isdecimal() is True, but "Hello123".isdecimal() is False because of the letters.

💡
Beginner Tip

isdecimal() checks characters, not whether the whole string is a valid number. "3.14".isdecimal() is False because the dot is not a digit. Use isdecimal() for whole-number strings only.

📝 Syntax

The isdecimal() method takes no parameters:

python
string.isdecimal()

Syntax Rules

  • string — any valid Python str object.
  • Return valueTrue if all characters are decimal digits and the string is non-empty; otherwise False.
  • No arguments — passing arguments raises TypeError.
  • Read-only — the original string is never modified.
  • Unicode — includes decimal digits from other scripts (like Arabic-Indic ٠١٢), not just ASCII 09.

↩ Return Value

isdecimal() always returns a boolean. It returns True only when the string contains one or more characters and every character is a decimal digit. It returns False for empty strings, strings with letters, and strings with punctuation such as ., -, or spaces.

python
print("12345".isdecimal())   # True
print("Hello123".isdecimal()) # False (letters)
print("".isdecimal())          # False (empty)
print("3.14".isdecimal())      # False (decimal point)
print("-42".isdecimal())       # False (minus sign)

⚡ Quick Reference

ExpressionResult
"12345".isdecimal()True
"Hello123".isdecimal()False (letters)
"".isdecimal()False (empty)
"007".isdecimal()True (leading zeros OK)
"3.14".isdecimal()False (not whole number)
Basic
text.isdecimal()

True or False

Validate
if pin.isdecimal():

Digits-only guard

Convert
int(text)  # after check

Safe to parse

Compare
text.isdigit()

Broader digit check

Examples Gallery

Run these examples in any Python 3 interpreter. Each one shows a common numeric validation scenario.

📚 Getting Started

Check whether a string contains only decimal digits.

Example 1 — Basic isdecimal()

A string of ASCII digits passes the check.

python
num_str = "12345"
result = num_str.isdecimal()

print("Text:  ", num_str)
print("Result:", result)

How It Works

  • Each character in "12345" is a decimal digit.
  • The string is non-empty, so isdecimal() returns True.
  • After this check passes, int(num_str) is safe for whole numbers.

Example 2 — Letters Make It False

Any non-digit character causes isdecimal() to return False.

python
text = "Hello123"
result = text.isdecimal()

print("Text:  ", text)
print("Result:", result)

How It Works

The letters in "Hello" are not decimal digits. Even one invalid character makes the entire result False.

📈 Practical Patterns

Edge cases, PIN validation, and comparison with isdigit().

Example 3 — Empty String, Floats, and Signs

Common inputs that beginners expect to pass—but do not.

python
samples = ["", "007", "3.14", "-42", "12 34"]

for text in samples:
    print(repr(text), "→", text.isdecimal())

How It Works

  • Empty strings always return False.
  • Leading zeros like "007" are fine—each character is still a digit.
  • Decimal points, minus signs, and spaces are not decimal digits.

Example 4 — Validating a PIN Code

Use isdecimal() before converting user input to an integer.

python
def validate_pin(pin):
    if not pin.isdecimal():
        return False, "PIN must contain digits only."
    if len(pin) != 4:
        return False, "PIN must be exactly 4 digits."
    return True, "PIN accepted."

for candidate in ["1234", "12a4", "12345"]:
    ok, message = validate_pin(candidate)
    print(repr(candidate), "→", message)

How It Works

Combine isdecimal() with len() for complete validation. Check digits first, then length.

Example 5 — isdecimal() vs isdigit()

isdigit() accepts some characters that isdecimal() rejects.

python
samples = ["12345", "\u0661\u0662\u0663", "\u2460"]

for text in samples:
    label = repr(text)
    print(label)
    print("  isdecimal():", text.isdecimal())
    print("  isdigit():  ", text.isdigit())
    print()

How It Works

  • ASCII and Arabic-Indic digits pass both methods.
  • Circled digit (\u2460) passes isdigit() but not isdecimal().
  • For strict base-10 whole numbers, prefer isdecimal().

🚀 Common Use Cases

  • PIN and OTP validation — ensure codes contain digits only before processing.
  • Form fields — verify age, quantity, or ID fields are whole numbers.
  • Pre-conversion checks — call isdecimal() before int(text) to avoid ValueError.
  • Data cleaning — flag non-numeric values in imported CSV columns.
  • Menu choices — validate that user input is a numeric option index.

🧠 How isdecimal() Works

1

Python reads the string

You call isdecimal() on a str object—literal or variable.

Input
2

Each character is checked

Python tests whether every character is a Unicode decimal digit (Nd). Empty strings fail immediately.

Scan
3

Boolean result is returned

True if all pass; False if any character fails or the string is empty.

Result
=

Validation complete

Use the boolean to accept input or show an error message.

📝 Notes

  • Decimal points, minus signs, plus signs, and spaces all cause False.
  • An empty string "" always returns False—there must be at least one digit.
  • Unicode decimal digits from other scripts (like Arabic-Indic) are accepted, not just ASCII 09.
  • Superscript and other compatibility digits pass isdigit() but often fail isdecimal().
  • isdecimal() does not check length—combine it with len() for fixed-width codes.

Conclusion

The isdecimal() method is a precise way to verify that a string contains only decimal digits. It returns a clear boolean result, which makes it ideal for validating whole-number input before parsing or storing data.

Remember that it checks individual characters, not full numeric expressions. For floats or negative numbers, you need different validation—or use isdigit() when a broader digit check is enough.

💡 Best Practices

✅ Do

  • Use isdecimal() for whole-number string validation
  • Combine with len() for PINs and fixed-length codes
  • Call isdecimal() before int() on user input
  • Prefer isdecimal() over isdigit() for strict base-10 checks
  • Give clear error messages when validation fails

❌ Don’t

  • Expect "3.14" or "-5" to pass isdecimal()
  • Assume an empty string returns True
  • Pass arguments to isdecimal()
  • Use it alone for float validation
  • Confuse isdecimal() with isdigit() without testing edge cases

Key Takeaways

Knowledge Unlocked

Five things to remember about isdecimal()

Use these points when validating numeric strings in Python.

5
Core concepts
02

True / False

Boolean return only.

Return
🛠 03

Zero Arguments

s.isdecimal()

Syntax
🔐 04

PIN Checks

Whole numbers only.

Use case
05

vs isdigit()

Stricter digit rules.

Compare

❓ Frequently Asked Questions

isdecimal() returns True if every character in the string is a decimal digit and the string is not empty. It returns False if the string contains letters, spaces, punctuation, symbols, or has no characters at all.
string.isdecimal(). It takes no arguments and returns a boolean (True or False).
It returns False. An empty string has no decimal characters, so isdecimal() is always False for "".
No. "3.14".isdecimal() and "-5".isdecimal() both return False because "." and "-" are not decimal digits. isdecimal() checks each character individually, not whether the whole string parses as a float.
Both require all characters to be digit-like and the string to be non-empty. isdecimal() is stricter: it allows only characters that can form base-10 numbers (like 0-9 and Arabic-Indic digits). isdigit() also accepts some compatibility characters such as superscript digits, which isdecimal() rejects.
No. isdecimal() only inspects the string and returns a boolean. The original string stays unchanged.

Explore More Python String Methods

Continue with isdigit(), isalpha(), and the rest of the string method reference.

Next: isdigit() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

17 people found this page helpful