How can Python convert a string to a float?

You can use the float() function to convert a string into a floating-point number.

The demo code is as follows:

str_num = "3.14"
float_num = float(str_num)
print(float_num)

The result is output as:

3.14

In the above example, a string variable str_num is first defined with a value of “3.14”. Then, the float() function is used to convert this string into a floating-point number and assign the conversion result to the variable float_num. Finally, the value of float_num, which is 3.14, is printed using the print() function.

bannerAds