Python isdigit() Explained: Check if a String Contains Only Digits (with examples)
Hello, this is BlockDMask.
Today, we will explore how to check whether a string contains only digits in Python using the isdigit() method.
This is very useful when you're validating user input, processing files, or scraping web data where numeric checks are needed.
Table of Contents
- 1. What is isdigit() in Python?
- 2. Limitations and common pitfalls of isdigit()
- 3. Practical examples of isdigit()
- 4. Difference between isdigit(), isdecimal(), and isnumeric()
1. What is isdigit() in Python?
The isdigit()
method checks if a string contains only numeric digits (0–9).
It belongs to the string class and can be used like this:
str.isdigit()
Basic behavior:
- Returns
True
if all characters are digits (0-9) - Returns
False
if any non-digit character exists - Signs like
-
(negative) or.
(decimal) make it returnFalse
- Certain Unicode numeric characters may return
True
(e.g. superscripts, fractions)
Typically used for checking whether user input contains a pure positive integer.
2. Limitations and common pitfalls of isdigit()
"3.14".isdigit()
→ False (cannot detect decimals)"-42".isdigit()
→ False (cannot detect negatives)"⅔".isdigit()
→ True (Unicode fraction accepted)
👉 In real-world input validation, isdigit()
is often not enough.
A more reliable way to check full integer inputs:
def is_integer(s):
try:
int(s)
return True
except ValueError:
return False
This works for both positive and negative integers.
3. Practical examples of isdigit()
Let’s look at various strings:
samples = [
"PythonRocks", # letters only
"456Data", # mixed digits & letters
"789012", # pure digits
"-789", # negative number
"12.34", # decimal number
"5²", # superscript 2 (unicode)
"⅕", # unicode fraction
"0", # zero
"007", # leading zeros
]
for s in samples:
print(f"'{s}'.isdigit() : {s.isdigit()}")
Expected Output:
'PythonRocks'.isdigit() : False
'456Data'.isdigit() : False
'789012'.isdigit() : True
'-789'.isdigit() : False
'12.34'.isdigit() : False
'5²'.isdigit() : True
'⅕'.isdigit() : True
'0'.isdigit() : True
'007'.isdigit() : True
🔎 Real-world usage example: validating user input
user_input = input("Enter your age: ")
if user_input.isdigit():
print("Valid age input.")
else:
print("Please enter digits only.")
✅ But remember: this only works for positive integers!
4. Difference between isdigit(), isdecimal(), and isnumeric()
Python provides 3 very similar methods that often confuse beginners. Here’s a simple comparison:
Method | Allowed Characters | Example |
---|---|---|
isdecimal() | Only decimal digits (0–9) | "123" → True |
isdigit() | Decimal digits + some unicode numerics | "⅕" → True |
isnumeric() | Widest coverage: numerals, fractions, unicode numerics | "二" (Chinese 2) → True |
✅ Usually for most applications, isdecimal()
is the strictest and safest for purely numeric inputs.
In this post, we learned how isdigit()
works in Python, where it’s useful, its limitations, and how it differs from similar methods.
Thanks for reading!
Comments
Post a Comment