Convert string to float Python | Example code

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:

Convert string to float Python

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.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.