Python String isdigit() Method

Short answer
str.isdigit() returns True if the string is not empty and every character is a digit. "123" passes; "-5", "3.14" and "" do not.

isdigit() is the quick check before converting text to a number with int(), for instance when validating an age or a PIN typed into input(). Remember what it does not accept: no minus sign, no decimal point and no spaces. For those cases, a try/except around int() or float() is more reliable; see convert string to int.

Quick facts
Syntaxtext.isdigit()
ParametersNone
ReturnsTrue if all characters are digits and the string is not empty, else False
Empty stringFalse
Relatedisdecimal(), isnumeric(), isalpha(), isalnum()

What isdigit() accepts and rejects

Only the characters 0 to 9 (and similar digit characters from other scripts) pass. Anything else, including signs, dots, commas and spaces, makes the result False.

Python
for s in ["2024", "-5", "3.14", "1 000", "", "٣٤"]:
    print(repr(s), s.isdigit())

The last value is the Arabic-Indic digits three and four, which are digits in Unicode, so the result is True.

isdigit() vs isdecimal() vs isnumeric()

The three methods differ for unusual Unicode characters. For everyday ASCII digits they agree.

Test string`isdecimal()``isdigit()``isnumeric()`
"123"TrueTrueTrue
"²" (superscript two)FalseTrueTrue
"½" (one half)FalseFalseTrue
"Ⅷ" (Roman numeral)FalseFalseTrue
"12.5"FalseFalseFalse

Validating input before int()

A safe pattern is to check isdigit() and then convert. Be aware that "²".isdigit() is True but int("²") raises ValueError. If you must be strict, use isdecimal(), or just catch the error from int(). For negative numbers isdigit() is not enough, so strip the sign yourself or use try/except, described in the ValueError lesson.

Examples you can run

Press Run, then edit the code and run it again.

Check simple strings

The basic cases, including the empty string.

Check simple strings
for s in ["42", "42a", "4 2", ""]:
    print(repr(s), s.isdigit())
Expected output'42' True
'42a' False
'4 2' False
'' False

Validate an age typed by the user

The program checks the text first and only then calls int().

Validate an age typed by the user
age = input("Age: ")
if age.isdigit():
    print("Next year:", int(age) + 1)
else:
    print("Please type digits only.")
Expected outputAge: 25
Next year: 26

Reject a decimal or negative value

The same check refuses text that float() or int() would understand.

Reject a decimal or negative value
for s in ["-7", "7.5", "75"]:
    print(s, s.isdigit())
Expected output-7 False
7.5 False
75 True

Handle negatives with try/except

int() itself decides what is valid, so it covers signs and surrounding spaces.

Handle negatives with try/except
def to_int(text):
    try:
        return int(text)
    except ValueError:
        return None

print(to_int("-7"), to_int(" 12 "), to_int("7.5"))
Expected output-7 12 None

Common mistakes

Expecting negative numbers to pass

✗ Wrong

print("-12".isdigit())
Prints False because `-` is not a digit.

✓ Right

s = "-12"
print(s.lstrip("-").isdigit())

Use try/except ValueError around int(), or check s.lstrip("-").isdigit() if only a leading minus is allowed.

Calling isdigit() on an int

✗ Wrong

print(123.isdigit())
SyntaxError: invalid decimal literal

✓ Right

print(str(123).isdigit())

isdigit() is a string method. Convert with str() first, or work with the number directly.

Treating an empty string as digits

✗ Wrong

text = ""
if text.isdigit() or text == "":
    print("ok")
Confusing logic: `"".isdigit()` is `False`, so you must handle emptiness on its own.

✓ Right

text = ""
print(bool(text) and text.isdigit())

Check for emptiness explicitly, because isdigit() returns False for it.

Python String isdigit() Method: questions people ask

How do I check if a string is a number in Python?

For whole non-negative numbers use s.isdigit(). For general numbers, including negatives and decimals, try float(s) inside try/except ValueError.

Why does isdigit() return False for negative numbers?

Because the minus sign is not a digit character, and isdigit() requires every character to be a digit.

What is the difference between isdigit() and isdecimal()?

isdecimal() accepts only decimal digits that int() can read; isdigit() also accepts characters such as superscript digits.

Keep learning

Step-by-step lessons:
Python CastingPython StringsPython String Methods

Related:
Python String isalpha() MethodPython String isalnum() MethodHow to Convert a String to an Int in PythonHow to Convert a String to a Float in Python

More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() Method See all →