Convert String to Float in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting the string to float. In Python, we can use float() to convert String to float.
Float()
This function is used to convert any data type to a floating-point number.
Syntax:
float(x)
The method only accepts one parameter and that is also optional to use. If no argument is passed then the method returns 0.0.
Example 1:
Python3
string= "3.141" print(string)print(type(string)) # converting string to floatFloat = float(string) print(Float)print(type(Float)) |
Output:
3.141 <type 'str'> 3.141 <type 'float'>
Example 2:
Python3
string = '55.567' float = float(string) print(type(float))print('Float Value =', float) |
Output:
<type 'float'>
('Float Value =', 55.567)Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


