Python float() Function Explained
The built-in float() function converts a value into a floating-point number.
This is useful when you need decimal numbers in Python, especially when working with user input, file data, or numeric strings. In this guide, you will learn what float() does, what values it accepts, what errors it can raise, and how to use it safely.
Quick example #
print(float(5))
print(float("3.14"))
print(float(True))
Output:
5.0
3.14
1.0
Use float() to convert compatible values like integers, numeric strings, and booleans into floating-point numbers.
What float() does #
float() converts a value to a floating-point number.
A floating-point number is a number that can contain a decimal part, such as:
5.03.140.5
A few key points:
float()returns a value of typefloat- The result is always a floating-point number
- It is often used to turn text into a number
- It is especially useful with values returned by
input()
Example:
number = float("2.75")
print(number)
print(type(number))
Output:
2.75
<class 'float'>
Basic syntax #
The basic syntax is:
float(value)
In most code, you pass one argument to float().
If you call float() with no argument, Python returns 0.0:
print(float())
Output:
0.0
You can print the result directly or store it in a variable:
price = float("19.99")
print(price)
Values float() can convert #
float() can convert several common value types.
Integers #
An integer becomes a float with .0 added:
print(float(5))
print(float(-2))
Output:
5.0
-2.0
Numeric strings #
Strings containing valid numbers can be converted:
print(float("3.14"))
print(float("10"))
Output:
3.14
10.0
Strings with spaces #
Extra spaces around the number are usually fine:
print(float(" 8.5 "))
Output:
8.5
Booleans #
Boolean values also work:
print(float(True))
print(float(False))
Output:
1.0
0.0
Existing floats #
If the value is already a float, float() returns a float version of it:
print(float(4.2))
Output:
4.2
Values that cause errors #
Some values cannot be converted by float().
Non-numeric text #
If the string does not contain a valid number, Python raises a ValueError:
print(float("hello"))
Error:
ValueError: could not convert string to float: 'hello'
This also happens with an empty string:
print(float(""))
Unsupported types #
Some types are not accepted directly, such as lists and dictionaries:
print(float([1, 2, 3]))
Error:
TypeError: float() argument must be a string or a real number, not 'list'
Strings with commas #
In normal Python code, a comma is not treated as a decimal point:
print(float("3,14"))
This raises a ValueError.
If you are fixing this specific problem, see how to convert a string to float in Python and ValueError: could not convert string to float.
Return value #
float() returns a new float value.
It does not change the original value in place.
Example:
text = "6.5"
number = float(text)
print(text)
print(number)
print(type(text))
print(type(number))
Output:
6.5
6.5
<class 'str'>
<class 'float'>
In real programs, you usually assign the result to a variable so you can use it later.
Common beginner use cases #
Convert input() text into a decimal number #
The input() function always returns a string. If you want to do math, you often need float().
user_value = input("Enter a price: ")
price = float(user_value)
print(price * 2)
If the user enters 4.5, the output will be:
9.0
For more help with this, see how to convert user input to numbers in Python.
Prepare values for math #
a = "2.5"
b = "1.5"
total = float(a) + float(b)
print(total)
Output:
4.0
Convert whole numbers to floats #
Sometimes a function or calculation expects float values:
count = 7
result = float(count)
print(result)
Output:
7.0
Convert numbers read from files or APIs #
Data often arrives as strings. float() helps turn that text into usable numbers.
temperature_text = "21.8"
temperature = float(temperature_text)
print(temperature)
Special values #
float() can also create some special floating-point values.
Positive infinity #
print(float("inf"))
Output:
inf
Negative infinity #
print(float("-inf"))
Output:
-inf
Not a number #
print(float("nan"))
Output:
nan
These values are valid floats, but they can be confusing at first:
infmeans positive infinity-infmeans negative infinitynanmeans “not a number”
Beginners usually do not need these right away, but it is helpful to know that float() accepts them.
Common errors and fixes #
The most common problems with float() happen when the value is not in the right format.
ValueError #
A ValueError happens when the value is the right general type, usually a string, but the string is not a valid number.
Common causes:
- Passing non-numeric text like
"abc" - Trying to convert an empty string
- Using a comma instead of a decimal point
- Forgetting that
input()returns a string
Example:
value = "abc"
print(float(value))
TypeError #
A TypeError happens when the value type cannot be converted directly.
Common causes:
- Passing a list
- Passing a dictionary
- Passing another unsupported type
Example:
value = {"price": "9.99"}
print(float(value))
Clean the string first #
If the string may contain extra spaces, strip() can help:
value = " 7.25 "
clean_value = value.strip()
print(float(clean_value))
Output:
7.25
Use debugging prints #
If conversion fails, these checks are useful:
value = " 3.5 "
print(value)
print(type(value))
print(repr(value))
clean_value = value.strip()
print(float(clean_value))
Why this helps:
print(value)shows the visible contentprint(type(value))shows the value typeprint(repr(value))reveals hidden spaces or charactersstrip()removes spaces at the start and end
Use try-except for unsafe input #
If the value may not be valid, handle the error safely:
value = input("Enter a number: ")
try:
number = float(value)
print("Converted:", number)
except ValueError:
print("That is not a valid number.")
If you are dealing with this exact error, see ValueError: could not convert string to float.
float() vs int() #
Both float() and int() convert values to numbers, but they do not behave the same way.
float() creates decimal numbers:
print(float("3.9"))
Output:
3.9
int() creates whole numbers:
print(int("10"))
Output:
10
A very common beginner mistake is expecting int("3.9") to work:
print(int("3.9"))
This raises a ValueError because "3.9" is not a valid integer string.
So:
float("3.9")worksint("3.9")fails
If you want a full comparison, see Python int() Function Explained. You may also find Python numbers explained: int, float, complex helpful.
FAQ #
What does float() return in Python? #
It returns a floating-point number, such as 5.0 or 3.14.
Can float() convert a string? #
Yes, if the string contains a valid number like "2.5" or "10".
Why does float(“abc”) fail? #
Because "abc" is not a valid numeric string, so Python raises a ValueError.
What does float() with no argument return? #
It returns 0.0.
Can float() convert True and False? #
Yes. True becomes 1.0 and False becomes 0.0.