Convert the String to a Float in Python
In this tutorial, we are going to learn about how to convert the string to a float in Python with the help of examples.
Using float() function
We can convert a string to a float by using the built-in float()
function in Python.
The float()
function takes the string as an argument and converts it to the float.
Here is an example:
price = "953.343"
print(float(price))
Output:
953.343
You can also convert array of strings to the float like this.
x = ["525.0", "125.6", "929.2"]
print(list(map(float, x)))
Output:
[525.0, 125.6, 929.2]