What is float() in Python?

The float() method in Python produces a floating-point number from a string or an integer.

Syntax

This method is declared as follows:

The syntax of float() method in Python

Parameter and return value

The float() method takes only one parameter, which is optional. This parameter can be a string or an integer.

The return value is a floating-point number. If a string needs to be passed in the float() method, it must constitute all numeric characters in it. In the case of no parameter, it returns 0.

Example

In the example below, for integers and strings with numeric characters, the float() method returns floating-point numbers. With non-numeric strings, this method raises an error.

#No parameter
print (float())
# Integer
print (float(10))
# String
print(float("13.33"))
# String with numeric characters and no decimal point
print(float("17"))

Free Resources