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.
Fundamentals
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.
Foundation
📝 Syntax
The isdigit() method takes no parameters:
python
string.isdigit()
Syntax Rules
string — any valid Python str object.
Return value — True 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.
Reference
↩ 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.
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))
📤 Output:
'10' → Double of 10 is 20
'10.5' → Please enter digits only.
'ten' → Please enter digits only.
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()
Circled digit ① and superscript ² pass isdigit() but not isdecimal().
For strict base-10 whole numbers, prefer isdecimal().
Applications
🚀 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.
Important
📝 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.
Wrap Up
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().
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about isdigit()
Use these points when validating numeric strings in Python.
5
Core concepts
🔢01
Digits Only
Every char a digit.
Purpose
✅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.