Use Python’s built-in float() to convert a numeric string to a floating-point value: value = float("3.14"). Python does not have a separate built-in type named double; float is the usual type for this purpose.
Convert a numeric string with float()
Pass the string to float(). The result is a Python float:
As an Amazon Associate I earn from qualifying purchases.
value = float("3.14")
print(value) # 3.14
The accepted syntax includes an optional sign, decimal-point notation, and an exponent. Leading and trailing whitespace is allowed, so this works too:
value = float(" -1.25e2 ")
The string form and conversion rules are documented in Python’s built-in functions reference.
#1 Best Overall
Handle invalid input
If the text does not match a valid number, float() raises ValueError. Catch it when the value comes from user input or another source that may be malformed:
user_text = input("Enter a number: ")
try:
value = float(user_text)
except ValueError:
print("Enter a valid number")
This keeps invalid text from stopping the program unexpectedly. A comma decimal such as "3,14" is not the documented decimal-point form; if your application accepts locale-specific input, normalize it according to the relevant locale before conversion rather than assuming float() will interpret the comma as a decimal separator.
Rank #2
Know which strings produce special values
float() accepts the case-insensitive spellings "nan", "inf", and "-inf", producing NaN and positive or negative infinity. These are valid floating-point values, but they may not be appropriate for every calculation or downstream system. Decide explicitly whether your application should allow them.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Choose float or Decimal
Use float for general numeric work
For ordinary scientific, engineering, and general calculations, float is usually the right conversion. Python’s floats use binary floating-point representation with 53 bits of precision. Many decimal fractions, including 0.1, cannot be represented exactly as binary fractions, so arithmetic can expose small representation effects. Python explains this in its floating-point tutorial.
Use Decimal when decimal arithmetic matters
For financial values or other cases where decimal digits and decimal-oriented arithmetic matter, construct a Decimal directly from the original string:
from decimal import Decimal
amount = Decimal("3.14")
Do not convert the string to float first if you want to preserve its written decimal value. Decimal(float(text)) captures the float’s existing binary approximation, which can produce extra digits. The Python decimal documentation demonstrates the difference between Decimal('0.1') and Decimal(0.1).
Quick Recap
Best Value
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.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Clear out junk files and repair common Windows errors3Scan for outdated or missing drivers - takes under a minute




