iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use Python’s built-in float() to convert a numeric string to a floating-point value: value = float("3.14"). Python’s ordinary built-in type for this purpose is called float, not double. If the text comes from a user, catch ValueError so invalid input does not stop your program.
Convert the string with float()
Pass the numeric text to float() and assign the returned value:
value = float("3.14")
print(value) # 3.14
Python’s built-in float is the standard floating-point type used for this conversion. The Python built-in functions documentation says a string argument should contain a decimal number, optionally preceded by a sign and optionally surrounded by whitespace.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Which string formats does float() accept?
The documented syntax supports signed decimal values and exponent notation. Leading and trailing whitespace is allowed:
#1 Best Overall
float(" -1.25e2 ") # -125.0
float() also accepts the special spellings "nan", "inf", and "-inf", producing NaN and positive or negative infinity. Those values may not be appropriate for later calculations, so validate them if your application requires finite numbers.
A comma is not the documented decimal-point separator: float("3,14") raises ValueError. If your input uses locale-specific number formatting, handle that format explicitly before conversion rather than assuming float() will interpret it as a decimal point.
Rank #2
Handle invalid user input
Text that does not match an accepted numeric form raises ValueError. Catch it when converting text that may be malformed:
user_text = input("Enter a number: ")
try:
value = float(user_text)
except ValueError:
print("Enter a valid number")
else:
print(value)
This keeps a bad entry from terminating the conversion flow and gives your program a place to request corrected input or report the problem.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Use Decimal when decimal arithmetic matters
Binary floating-point is suitable for many general, scientific, and engineering calculations, but decimal fractions such as 0.1 generally cannot be represented exactly in binary. That can lead to small representation effects in calculations; Python’s floating-point tutorial explains the limitation and notes that its usual floating-point values have 53 bits of precision.
For decimal-oriented arithmetic, such as working with financial amounts, 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 preserving the written decimal value is important. Decimal(float("0.1")) captures the exact value of the already-approximated binary float, rather than the decimal value represented by the text. The Python decimal documentation illustrates the difference between constructing from "0.1" and from 0.1.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.

