How to Convert String to Float in Python
To convert a string to a float in Python, use the built-in float() function.
This is useful when you have text like "3.14" and want to turn it into a number you can calculate with. It is also common when working with input(), form data, or values read from files.
Quick answer
text = "3.14"
number = float(text)
print(number)
print(type(number))
Output:
3.14
<class 'float'>
Use float() when the string contains a valid number such as "3.14", "10", or "-2.5".
Use float() to convert a string
The main tool for this job is float().
It takes numeric text and converts it into a floating-point number.
a = float("3.14")
b = float("10")
c = float("-7.5")
d = float("1e3")
print(a)
print(b)
print(c)
print(d)
Output:
3.14
10.0
-7.5
1000.0
A few important things to notice:
"3.14"becomes3.14"10"becomes10.0"-7.5"becomes-7.5"1e3"becomes1000.0- The result is always a
float
If you want a full explanation of how this function works, see Python float() function explained.
What valid float strings look like
float() only works if the string is in a format Python understands.
These are valid examples:
print(float("10")) # whole number string
print(float("3.5")) # decimal number
print(float("-2.75")) # negative number
print(float("1e6")) # scientific notation
print(float(" 4.2 ")) # spaces around the value
Output:
10.0
3.5
-2.75
1000000.0
4.2
Common valid patterns:
- Digits only:
"10"→10.0 - Decimal numbers:
"3.5"→3.5 - Negative numbers:
"-2.75"→-2.75 - Scientific notation:
"1e6"→1000000.0 - Leading and trailing spaces are usually allowed
Handle invalid input safely
If the string is not a valid number, float() raises a ValueError.
text = "hello"
number = float(text)
This causes an error because "hello" is not numeric text.
When the value may come from a user, a file, or another unreliable source, use try and except:
text = "hello"
try:
number = float(text)
print("Converted:", number)
except ValueError:
print("That is not a valid number.")
Output:
That is not a valid number.
This is a better approach because your program does not crash. Instead, it shows a clear message.
If you are seeing this error already, read how to fix ValueError: could not convert string to float.
Clean the string before converting
Sometimes the value is almost correct, but it needs a little cleaning first.
Remove extra spaces
Use strip() to remove spaces at the beginning and end:
value = " 3.14 "
cleaned = value.strip()
print(float(cleaned))
Output:
3.14
Handle commas carefully
Strings like "1,234.56" are not valid for float() as-is.
value = "1,234.56"
cleaned = value.replace(",", "")
print(float(cleaned))
Output:
1234.56
Only remove commas if you expect them as formatting characters.
Currency symbols do not work directly
float() cannot read values like "$19.99" without cleaning them first.
value = "$19.99"
cleaned = value.replace("$", "")
print(float(cleaned))
Output:
19.99
In real programs, decide whether you want to:
- clean the data first
- reject invalid input and show an error
Convert user input to float
The input() function always returns a string.
So if the user types a number, you still need to convert it.
number = float(input("Enter a number: "))
print("You entered:", number)
If the user types 3.5, the result will be a float.
Because users can type unexpected values, it is safer to add error handling:
text = input("Enter a number: ")
try:
number = float(text)
print("You entered:", number)
except ValueError:
print("Please enter a valid number.")
This pattern is very common in beginner programs.
To learn more, see Python input() function explained and how to convert user input to numbers in Python.
Know when to use int() instead
Use float() when you need decimal values.
Use int() when you need whole numbers only.
print(float("5"))
print(int("5"))
Output:
5.0
5
But this does not work:
print(int("3.14"))
That raises an error because "3.14" is not a whole-number string.
If you specifically need whole numbers, see how to convert a string to an integer in Python.
Common mistakes
These are some common reasons string-to-float conversion fails:
- Trying to convert text like
"hello"withfloat() - Using numbers with commas such as
"1,234.56"without cleaning the string - Passing currency text like
"$19.99"tofloat() - Assuming
input()returns a number automatically - Using
int()instead offloat()for decimal strings
If you are debugging a problem, these quick checks can help:
print(value)
print(type(value))
print(repr(value))
cleaned = value.strip()
print(cleaned)
Why these help:
print(value)shows the visible contentprint(type(value))confirms whether it is really a stringprint(repr(value))shows hidden spaces and special charactersstrip()removes extra spaces before conversion
FAQ
Does float() work on whole numbers stored as strings?
Yes. float("10") returns 10.0.
Why does float() fail on "3,14"?
Python expects a dot as the decimal separator in standard float strings.
Can float() convert user input directly?
Yes. Example:
number = float(input("Enter a number: "))
What error happens if the string is not a number?
Python raises ValueError.
Should I use float() or int()?
Use float() for decimal values and int() for whole-number strings.