To check if a string is a number in Python, the most reliable approach is to try converting it with int() or float() and catch ValueError. The string methods isdigit(), isnumeric() and isdecimal() only test whether every character is a certain kind of digit, and they reject signs, decimal points and exponents. Knowing the difference between python isdigit isnumeric isdecimal and a try/except check prevents subtle bugs in input validation.
This guide uses Python 3.13 and 3.14 and covers what each method accepts, why "-5".isdigit() is False, how to validate integers and floats, and how to handle special cases such as nan, inf and Unicode digits.
All three are string methods that return True only when the string is non-empty and every character passes the test. They form a hierarchy: every decimal character is a digit, and every digit is numeric.
| Method | Accepts | Example True | Example False |
|---|---|---|---|
isdecimal() | Characters that can form base-10 numbers (Unicode category Nd) | "123", "١٢٣" | "²", "½" |
isdigit() | Decimal characters plus digit-like ones such as superscripts | "123", "²" | "½" |
isnumeric() | Digits plus other numeric characters such as fractions and Roman numerals | "½", "Ⅷ", "五" | "1.5", "abc" |
Notice that "4.2", "-7" and " 5" fail all three. These methods look at characters, not numeric syntax, so a minus sign, a decimal point or a space makes them return False.
isdigit() returns True for characters such as "²" (superscript two), but int("²") raises ValueError. Using isdigit() as a gate before int() can therefore still crash.
isdecimal() is the one that matches what int() accepts for digits. For example, int("١٢٣") returns 123, because Arabic-Indic digits are decimal characters. If you only want ASCII digits, say so explicitly with s.isascii() and s.isdecimal(), or use re.fullmatch(r"[0-9]+", s).
The most dependable check is to attempt the conversion. Python programmers call this EAFP: easier to ask forgiveness than permission.
Each function returns True only when the conversion succeeds.
Be aware of what int() and float() accept:
int(" 42 ") is 42.int("1_000") is 1000."+5" and "-5".float() also accepts "nan", "inf" and "-infinity" in any case.If nan or inf should count as invalid, add a check with math.isfinite():
When you must accept only a specific format, for example plain decimals with no spaces, underscores or exponents, use re.fullmatch.
In Python 3, \d matches any Unicode decimal digit by default. Add the re.ASCII flag, or use [0-9], if you want ASCII digits only.
Choose by what you need to accept:
s.isascii() and s.isdecimal().int(s).float(s), plus math.isfinite() if needed.decimal.Decimal(s) inside a try block, which raises decimal.InvalidOperation on bad input.re.fullmatch.Also remember that all the checks are for strings. If the value may already be a number, isinstance(x, (int, float)) is the correct test, and note that bool is a subclass of int.
The minus sign is not a digit, so the string contains a non-digit character. Use int() in a try block, or strip a leading sign yourself.
isdecimal() accepts only characters that form base-10 numbers. isdigit() also accepts characters such as superscripts, which int() cannot convert.
No. The dot is not a numeric character, so "3.14".isnumeric() is False. Use float() for that.
Call int(s) inside try/except ValueError. It handles signs, whitespace and underscores for you.
isdecimal(), isdigit() and isnumeric() test characters, not number syntax, and they reject signs and decimal points. Superscripts pass isdigit() but fail int(), so do not use it as a guard. For real validation, attempt int() or float() and catch ValueError, add math.isfinite() to exclude nan and inf, use Decimal for exact values, and use re.fullmatch for strict formats.