What is the Python float() method?

The Python float() function is a built-in Python function that converts the given value (number or string) into a floating-point number and returns the result. If the float() method fails to convert string to float then it raises the ValueError.

The Syntax of float() is:

float(Parameter value)

Parameter Value:

A number or a string that can be converted into a floating-point number.

Parameter is optional. If there is no parameter then it returns 0.0.

Note: If the value is a string then it should contain a decimal point and the valid argument should contain some numeric value.

Return Values

The float() function returns a floating-point value equivalent to the number passed as is or in the form of a string.

Errors

This method raises exceptions when it receives invalid parameter value.

  1. ValueError
  2. TypeError

ValueError – Python ValueError is raised when a Python function receives an argument of the correct type but a wrong value.

TypeError – Python TypeError is raised when a Python function receives an argument is not of the expected type

#python

Python float() Function Explained with Various Examples
1.05 GEEK