Use float() function to convert String to float in Python. This function takes any data type and converts it into a floating-point number.
float(string)
Example convert string to float Python
Simple example code just passes the string value into a float function.
s = "3.141"
# converting string to float
Float = float(s)
print(Float)
print(type(Float))
Output:
Note:
- The string should contain a float value else float() would return ValueError
- If the number is outside the range of a floating variable it would return OverflowError
- No value is passed as an argument it returns 0.0
Do comment if you have any doubts and suggestions on this Python string to float topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.