The str.isdecimal() method returns True if all characters in a string are decimal characters (base-10 digits) and the string contains at least one character. Otherwise, it returns False.
Key Distinction: isdecimal() is the strictest numeric validation method in Python. Unlike isdigit() and isnumeric(), it strictly rejects superscripts (e.g., ²), fractions (e.g., ½), and Roman numerals. It guarantees that the string can be safely parsed by int() without raising a ValueError.
string.isdecimal()
| Parameter | Type | Description |
|---|---|---|
| None | — | Takes no arguments. Called directly on any string instance. |
| Return Value | bool |
Returns True if all characters are base-10 decimal characters and the string is non-empty; returns False otherwise. |
Standard ASCII digits 0-9 return True. Any non-digit character—such as letters, punctuation, spaces, or decimal points—returns False:
# 1. Standard digits (0-9)
s1 = "12345"
print(f"\'{s1}\'.isdecimal():", s1.isdecimal())
# 2. Mixed alphanumeric string
s2 = "234ab"
print(f"\'{s2}\'.isdecimal():", s2.isdecimal())
# 3. Floating-point number (period is punctuation)
s3 = "234.45"
print(f"\'{s3}\'.isdecimal():", s3.isdecimal())
# 4. Negative integer (minus sign is punctuation)
s4 = "-10"
print(f"\'{s4}\'.isdecimal():", s4.isdecimal())
# 5. Empty string
s5 = ""
print(f"Empty string.isdecimal():", s5.isdecimal())
Output:
'12345'.isdecimal(): True
'234ab'.isdecimal(): False
'234.45'.isdecimal(): False
'-10'.isdecimal(): False
Empty string.isdecimal(): False
A critical reason to use isdecimal() instead of isdigit() is preventing runtime crashes when converting strings to integers using int():
"²" pass isdigit() as True, but calling int("²") immediately crashes with ValueError: invalid literal for int() with base 10: '²'. By contrast, "²".isdecimal() safely returns False.
superscript_str = "2²"
print("isdigit():", superscript_str.isdigit()) # Output: True
print("isdecimal():", superscript_str.isdecimal()) # Output: False
try:
val = int(superscript_str)
except ValueError as e:
print("int() conversion failed:", e)
Output:
isdigit(): True
isdecimal(): False
int() conversion failed: invalid literal for int() with base 10: '2²'
Decimal characters are defined by the Unicode General Category Nd (Decimal Number). This includes base-10 digits from writing systems worldwide, such as Arabic-Indic and Devanagari numerals. Crucially, Python's int() function can directly parse these into standard integers:
# Devanagari numerals: १२३ (123)
devanagari_num = "\u0967\u0968\u0969"
print("Devanagari text:", devanagari_num)
print("isdecimal():", devanagari_num.isdecimal()) # Output: True
print("Parsed integer:", int(devanagari_num)) # Output: 123
print("Type:", type(int(devanagari_num)))
Output:
Devanagari text: १२३
isdecimal(): True
Parsed integer: 123
Type: <class 'int'>
Use this reference table to choose the exact validation method for your requirements:
| String Input | Description | isdecimal() | isdigit() | isnumeric() |
|---|---|---|---|---|
"12345" | Standard ASCII digits (0–9) | ✔ True | ✔ True | ✔ True |
"\u0967\u0968\u0969" (१२३) | Devanagari base-10 digits | ✔ True | ✔ True | ✔ True |
"2²" (\u00B2) | Superscript exponent | ✘ False | ✔ True | ✔ True |
"½" (\u00BD) | Vulgar fraction | ✘ False | ✘ False | ✔ True |
"Ⅳ" (\u2163) | Roman numeral | ✘ False | ✘ False | ✔ True |
"一千二百三十四" | Chinese numerals | ✘ False | ✘ False | ✔ True |
"-50" | Negative integer | ✘ False | ✘ False | ✘ False |
"12.34" | Floating point | ✘ False | ✘ False | ✘ False |
When collecting postal PIN codes, credit card OTPs, or numerical account identifiers, isdecimal() provides the exact filter required to reject spaces, letters, and punctuation:
def validate_pin(pin: str) -> bool:
# Valid PIN must be strictly decimal digits and exactly 4 or 6 characters
return pin.isdecimal() and len(pin) in (4, 6)
print("validate_pin(\'500081\'):", validate_pin("500081")) # Output: True
print("validate_pin(\'1234\'):", validate_pin("1234")) # Output: True
print("validate_pin(\'12a4\'):", validate_pin("12a4")) # Output: False
print("validate_pin(\'12 34\'):", validate_pin("12 34")) # Output: False
Write an order verification routine that extracts only valid base-10 numerical quantity strings from user inputs, converts them safely to integers, and calculates total items sold:
order_inputs = ["5", "12", "2²", "N/A", "8", "10.5", "0"]
# Filter only true decimal inputs that can safely be converted to int
valid_quantities = [int(qty) for qty in order_inputs if qty.isdecimal()]
total_quantity = sum(valid_quantities)
assert valid_quantities == [5, 12, 8, 0]
assert total_quantity == 25
print("Valid Quantities:", valid_quantities)
print("Total Units Ordered:", total_quantity)
print("All unit assertions passed successfully!")
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.