Skip to content

Convert string to float Python | Example code

  • by

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

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading