Python float() Function

The Python float() is a built-in function that returns a floating point number constructed from a number or a string.

python float() function

Python float() Syntax



  • arg (optional) – is a number or string that is to be converted into floating point type.

If the argument is a string, it should contain a decimal number, optionally preceded by a sign( + or – ), and optionally embedded in whitespace.

float() function return values

The Python float() function returns:

  • 0.0 if no argument is passed
  • OverflowError if outside the range of floating point number
  • Corresponding floating point number for a valid argument

How to check if an object is floating point number?

Well, Python has built-in function type() which returns the type of object or simply the class to which the object belongs to.

>>> type(1)
<class 'int'>

>>> x  = float(1)
>>> type(x)
<class 'float'>

Python float() Function Example

>>> #integer
>>> float(1)

>>> #strings
>>> float('1')
>>> float('infinity')

>>> float(1e-0.02)

>>> float(+2E3)