Python String to float

In this tutorial, we will see how to convert String to float in python.

You can simply use float() function to convert String to float.
Let’s understand with the help of a simple example.

Output:

Type of f:
22.4543232

What happens if String cannot be converted to float

If String cannot be converted to float then it will raise ValueError.
Let’s understand with the help of example.

Output:

—————————————————————————
ValueError Traceback (most recent call last)
in ()
1 str=”abc”
—-> 2 f=float(str)
3 print(f)

ValueError: could not convert string to float: ‘abc’

That’s all about converting Python String to float.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *