Python float() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, float(x) converts x to a floating-point number: float("3.14") is 3.14 and float(7) is 7.0. With no argument it returns 0.0.

Strings may use scientific notation ("1e3"), surrounding whitespace, and the special words "inf", "-infinity", and "nan" in any case. Floats are binary approximations with about 15 to 17 significant digits, so some decimal values cannot be stored exactly.

float is a built-in type (a class) that you call like a function: each call creates a new float object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Convert an int or parse text.

python
value = float(x)
CallWhat it does
float("3.14")Returns 3.14.
float("1e3")Returns 1000.0.
float(7)Returns 7.0.
float("inf")Returns positive infinity.

Compare floats with math.isclose(a, b) instead of == when they come from calculations.

Library declarations
python
float(x=0.0, /)
# class float: float is a built-in type

Parameters

ParameterDescription
xOptional. An int, a float, a string, or an object with __float__() or __index__(). Default 0.0.

Return Value

Return ValueDescription
floatThe floating-point value, possibly inf or nan.

Python Examples

Example 1 — Convert Strings and Ints to float

Parse plain decimals, padded text, scientific notation, and whole numbers.

python
values = ["3.14", " 2.5 ", "1e3", "-0.001", "7"]

for text in values:
    print(repr(text), "->", float(text))
print(float(10), float())

How It Works

  1. Decimal text. "3.14" and "-0.001" become the floats you would expect.
  2. Spaces trimmed. " 2.5 " is accepted because surrounding whitespace is ignored.
  3. Scientific notation. "1e3" means 1 times 10 cubed, printed as 1000.0.
  4. Always a float. Whole numbers gain .0, and float() with no argument is 0.0.

Example 2 — Why 0.1 + 0.2 Is Not Exactly 0.3

See the classic float rounding error and three ways to deal with it.

python
import math

a = 0.1 + 0.2
print(a)
print(a == 0.3)
print(math.isclose(a, 0.3))
print(round(a, 2))
print(f"{a:.2f}")

How It Works

  1. Binary fractions. 0.1 and 0.2 cannot be stored exactly in binary, so their sum is a hair above 0.3.
  2. == is too strict. The tiny difference makes a == 0.3 False.
  3. Compare with tolerance. math.isclose() allows a small relative difference and returns True.
  4. Display cleanly. round(a, 2) or the .2f format shows the value people expect.

Example 3 — Infinity, NaN, and Invalid Input

Create the special float values, compare them, and catch strings that are not numbers.

python
import math

pos_inf = float("inf")
not_a_number = float("nan")

print(pos_inf > 10 ** 100)
print(-pos_inf)
print(not_a_number == not_a_number)
print(math.isnan(not_a_number))

for text in ["12.5kg", ""]:
    try:
        float(text)
    except ValueError:
        print(f"cannot convert {text!r}")

How It Works

  1. Infinity beats everything. float("inf") is greater than any int, even 10 to the 100th.
  2. Negative infinity. Negating it prints -inf.
  3. NaN is never equal. nan == nan is False, so use math.isnan() to detect it.
  4. Bad text. A unit suffix or an empty string raises ValueError; !r shows the exact input.

Frequently Asked Questions

Call float("3.14"). It accepts whitespace, a sign, scientific notation like "1e-3", and the words inf and nan.
Floats are stored in binary, and 0.1 and 0.2 have no exact binary form. The small errors show up in the sum. Use math.isclose() to compare or round() to display.
Use math.isnan(x). x == float("nan") is always False because NaN is not equal to itself.
Remove the thousands separator first: float("1,234.5".replace(",", "")). float() does not accept commas.
Use decimal.Decimal for money and other values that must be exact in base 10. Floats are faster and fine for measurements and science.

Next: format()

Format one value with a format spec like ".2f".

format() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful