Python – How to convert String to float

In Python, we can use float() to convert String to float.


num = "3.1415"

print(num)
print(type(num))  # str

pi = float(num)  # convert str to float

print(pi)
print(type(pi))  # float

Output


3.1415
<class 'str'>
3.1415
<class 'float'>

References

  1. Python docs – float()

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar