HOWTO · Python

How to Check if a String Is a Number in Python

Learn how to check whether a Python string can be converted to a float, handle ValueError, and understand why digit-only checks are narrower.

On this page

To check whether a string can be converted to a Python floating-point number, call float() and handle ValueError. This checks Python’s float syntax; an application may also need rules such as rejecting infinity or NaN.

Use the float() Function to Check if a String Is a Number in Python

Run this example:

print(float("22.23"))
22.23

float() accepts more than decimal strings. It can convert integer notation, signed values, exponent notation, and surrounding whitespace, such as "22", "-3.5", "1e3", and " 4.2 ". Text that does not follow float syntax, such as "developer" or "2.3.4", raises ValueError.

For data from a file or another external source, put the conversion in a small helper. Return the converted value when you need to use it; return None when conversion fails. This avoids trying to validate each value with a separate character test.

def parse_float(value):
    try:
        return float(value)
    except ValueError:
        return None


for value in ("22.23", "developer"):
    result = parse_float(value)
    print(result)
22.23
None

The helper returns the converted number when parsing succeeds and None otherwise. If a Boolean is more useful, use the same try/except ValueError pattern and return True after conversion or False in the handler. Catch ValueError for text that cannot be parsed; do not describe that case as OverflowError.

Successful conversion does not guarantee that a value is finite. Python’s float() can produce infinity or NaN from the corresponding supported spellings. If your application requires finite values only, check the converted result with math.isfinite() as a separate policy.

Use the isdigit() and partition() Functions to Check if a String Is a Number in Python

isdigit() answers a narrower question: whether every character in a string is a digit. It returns False for ordinary decimal notation, signs, and exponent notation, so it is not a general float validator. For example, "4546".isdigit() is true, while "4546.4646".isdigit() and "-12".isdigit() are false—even though float() can convert the latter two strings.

Use partition() only when you intentionally support a limited format that you define, such as digits, one decimal point, and more digits. It returns the text before the first separator, the separator itself, and the text after it. This makes it possible to check each part explicitly, while keeping the rule narrow and easy to review.

value = "4546.4646"
whole, point, fraction = value.partition(".")
is_simple_decimal = (
    value.count(".") == 1
    and point == "."
    and whole.isdigit()
    and fraction.isdigit()
)
print(is_simple_decimal)

This check accepts the shown digits-on-both-sides form. It rejects integer-only input, a leading or trailing decimal point, signs, surrounding whitespace, and exponent notation. Python’s float() accepts several of those forms, including integers, signed values, whitespace, and exponents, so this check is not equivalent to float parsing. Also, isdigit() recognizes some Unicode digits; use an explicit ASCII-digit rule if your input format requires ASCII only. A period is the decimal separator in Python float syntax; a string such as "4,2" is not converted as 4.2. If data uses a locale-specific separator, parse it according to that input format instead of silently replacing punctuation.

A hand-written check must define how to handle these choices and can easily drift from Python’s float syntax. Use float() with ValueError handling when the goal is to accept values Python can convert.

value = "4546.4646"
print(value.isdigit())
print(float(value))
False
4546.4646