Python isdigit() Method

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

What You’ll Learn

The isdigit() method returns True when every character in a string is a digit and the string is not empty. It is one of the most common ways to validate numeric input from users before converting strings to integers or performing calculations.

01

Digit Check

All chars are digits.

02

Returns bool

True or False result.

03

No Arguments

Call directly on str.

04

Empty = False

Zero-length fails check.

05

User Input

Validate numeric text.

06

vs isdecimal()

Broader digit rules.

Definition and Usage

In Python, isdigit() inspects each character in a string. If every character is a digit (including Unicode digit characters and some compatibility forms like superscript digits) and the string has at least one character, it returns True. Otherwise it returns False. For example, "12345".isdigit() is True, but "Hello123".isdigit() is False because of the letters.

💡
Beginner Tip

isdigit() checks characters, not whether the whole string is a valid number. "3.14".isdigit() and "-5".isdigit() both return False because . and - are not digits. Use it for whole-number strings of digit characters only.

📝 Syntax

The isdigit() method takes no parameters:

python
string.isdigit()

Syntax Rules

  • string — any valid Python str object.
  • Return valueTrue if all characters are digits and the string is non-empty; otherwise False.
  • No arguments — passing arguments raises TypeError.
  • Read-only — the original string is never modified.
  • Unicode — accepts digit characters from various scripts, plus some compatibility digits that isdecimal() rejects.

↩ Return Value

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

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

⚡ Quick Reference

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

True or False

Validate
if text.isdigit():

Digits-only guard

Convert
int(text)  # after check

Safe to parse

Compare
text.isdecimal()

Stricter base-10 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 digit characters.

Example 1 — Basic isdigit()

A string of ASCII digits passes the check.

python
numeric_string = "12345"
result = numeric_string.isdigit()

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

How It Works

  • Each character in "12345" is a digit.
  • The string is non-empty, so isdigit() returns True.
  • You can safely convert with int(numeric_string) after this check.

Example 2 — Letters Make It False

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

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

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

How It Works

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

📈 Practical Patterns

Edge cases, user input validation, and comparison with isdecimal().

Example 3 — Empty String, Floats, and Signs

Common inputs that look numeric but fail isdigit().

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

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

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 digit characters.

Example 4 — Validating User Input

Check numeric input before performing a calculation.

python
def double_if_numeric(value):
    if not value:
        return "Input cannot be empty."
    if not value.isdigit():
        return "Please enter digits only."
    return f"Double of {value} is {int(value) * 2}"

for entry in ["10", "10.5", "ten"]:
    print(repr(entry), "→", double_if_numeric(entry))

How It Works

Check for empty input first, then use isdigit() before calling int() to avoid ValueError.

Example 5 — isdigit() vs isdecimal()

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

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

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

How It Works

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

🚀 Common Use Cases

  • Menu choices — verify the user typed a numeric option before processing.
  • Quantity fields — ensure order counts contain digits only.
  • Pre-conversion checks — call isdigit() before int(text).
  • Game scores — validate that a score entry is numeric text.
  • Data filtering — identify digit-only columns in imported text data.

🧠 How isdigit() Works

1

Python reads the string

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

Input
2

Each character is checked

Python tests whether every character is a digit. 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.
  • isdigit() accepts some Unicode compatibility digits that isdecimal() rejects.
  • Negative numbers and floats fail because - and . are not digits.
  • isdigit() does not check length—combine it with len() for fixed-width codes.

Conclusion

The isdigit() method is a straightforward way to verify that a string contains only digit characters. It returns a clear boolean result, which makes it one of the most-used tools for validating numeric text input in Python.

Remember that it checks individual characters, not full numeric expressions. For strict base-10 validation, use isdecimal(); for letters mixed with digits, use isalnum().

💡 Best Practices

✅ Do

  • Use isdigit() for whole-number string validation
  • Check for empty strings before or alongside isdigit()
  • Call isdigit() before int() on user input
  • Use isdecimal() when you need strict base-10 digits only
  • Give clear error messages when validation fails

❌ Don’t

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

Key Takeaways

Knowledge Unlocked

Five things to remember about isdigit()

Use these points when validating numeric strings in Python.

5
Core concepts
02

True / False

Boolean return only.

Return
🛠 03

Zero Arguments

s.isdigit()

Syntax
💬 04

User Input

Validate before int().

Use case
05

vs isdecimal()

Broader digit rules.

Compare

❓ Frequently Asked Questions

isdigit() returns True if every character in the string is a 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.isdigit(). It takes no arguments and returns a boolean (True or False).
It returns False. An empty string has no digit characters, so isdigit() is always False for "".
No. "3.14".isdigit() and "-5".isdigit() both return False because "." and "-" are not digits. isdigit() checks each character individually, not whether the whole string parses as a number.
Both require all characters to be digit-like and the string to be non-empty. isdecimal() is stricter and allows only base-10 decimal digits. isdigit() also accepts compatibility characters like superscript and circled digits that isdecimal() rejects.
No. isdigit() only inspects the string and returns a boolean. The original string stays unchanged.

Explore More Python Tutorials

You’ve covered the string method series—continue with Python interview programs and pattern exercises.

Python Interview Programs →

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.

18 people found this page helpful