For text that should become a Python float, try float(text) and catch ValueError. If the result must be finite, also check math.isfinite(), because Python accepts spellings for NaN and infinity. If decimal representation matters, parse the original text with Decimal(text) and check is_finite().
How do I check if a string is a number in Python?
Use conversion rather than a string character test. float() attempts to parse the whole input as a floating-point value; malformed text raises ValueError.
import math
def is_finite_float_text(text: str) -> bool:
try:
return math.isfinite(float(text))
except ValueError:
return False
This function returns true only when the text converts to a finite float. If the application intentionally accepts NaN or infinity, return true after successful conversion instead of calling math.isfinite(). Python documents the accepted float spellings in its built-in types reference.
How do I validate decimal input in Python?
Use Decimal when decimal-oriented input semantics matter, and pass it the original string. Catch InvalidOperation for malformed input; call is_finite() if infinity and NaN should not count.
#1 Best Overall
from decimal import Decimal, InvalidOperation
def is_finite_decimal_text(text: str) -> bool:
try:
return Decimal(text).is_finite()
except InvalidOperation:
return False
Decimal('0.1') represents the decimal value written in the input. By contrast, Decimal(0.1) captures the exact value of the already-created binary float; Python’s documented example for Decimal(float('1.1')) is 1.100000000000000088817841970012523233890533447265625. See the decimal module documentation.
What syntax does Python accept?
A successful conversion does not mean text matches a particular form such as digits, a decimal point, and more digits. Python parsers accept forms including signs and exponents, and special values such as NaN and infinity. The Decimal string grammar also allows surrounding whitespace and underscores, and permits Unicode decimal digits.
Rank #2
If a form must meet a specific input contract—for example, no exponent notation, no whitespace, or ASCII digits only—define and validate that contract separately. A narrow expression such as ^[0-9]+.[0-9]+$ rejects integers, signed numbers, and exponent notation, even though a parser may accept them. Parsing answers whether conversion succeeds; policy validation answers whether the input is acceptable for your application.
Why not use isdecimal(), isdigit(), or isnumeric()?
These string methods classify characters; they do not parse general numeric notation. isdecimal() accepts Unicode decimal characters, including Arabic-Indic digits, but not signs or decimal points. isdigit() includes characters such as superscript two, and isnumeric() also recognizes values such as the vulgar fraction one fifth. Those characters are not necessarily valid decimal-number text for your application. Use these methods only when the field is deliberately limited to the relevant character category. See Python’s string type documentation.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Quick Recap
Best Value
Which method should I choose?
| Requirement | Approach | Important qualification |
|---|---|---|
| Convert ordinary input to a float | float(text) in a try/except ValueError |
NaN and infinity spellings can parse successfully. |
| Accept only finite floats | math.isfinite(float(text)) with conversion error handling |
Does not enforce a custom text format. |
| Preserve decimal input semantics | Decimal(text), then is_finite() as needed |
Pass the original string, not a float converted earlier. |
| Parse using known local decimal and grouping conventions | locale.atof(text) |
Interpretation follows the active LC_NUMERIC locale; see the locale documentation. |
| Check a digits-only field | Choose a character predicate that matches the field definition | These predicates do not validate signed or exponent notation. |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




