# Python float() Function – Convert String to Float

## How to Use the Python float() Function

In this tutorial, you'll learn how to use the float() function in Python. The float() function converts a number or string representing a number to a floating-point number.

### Python float() Function Syntax

The syntax of the float() function is as follows:

float(value)

### The float() Function Parameter

• value: A number or string representing a number

### Remarks

• float() return 0.0 if no parameter is given.
• If the value is a string, it can contain leading and trailing white spaces.

### Examples

#### Convert a String to a Floating-Point Number

The following sample code illustrates an example of converting a string representing a number to a floating-point number:

value = "120.534"
number = float(value)
print (number)

Output:

120.534

#### Convert a Numeric Number to a floating-point Number

The following code converts a numeric number to a floating-point number:

value = 124.9293
number = float(value)
print (number)

Output:

124.9293

#### Convert a String Representing a Negative Numeric Number

Example:

value = "-1e-003"
number = float(value)
print (number)

Output:

-0.001

#### Convert a String Representing a Positive Numeric Number

Example:

value = '+1E6'
number = float(value)
print (number)

Output:

1000000.0

#### Convert a String Containing Leading and Trailing Spaces

Sample code that converts a string containing leading and leading spaces to a floating-point number:

value = ' +1E6 '
number = float(value)
print (number)

Output:

+1000000.0

#### Convert a String Not Representing a Number

Sample code that converts a string not representing a number to a floating number:

value = ' NaN '
number = float(value)
print (number)

Output:

nan

#### Convert a String Representing Infinity

Example:

value = ' -INFINITY '
number = float(value)
print (number)

Output:

-inf

#### The float() Function without Argument

The following is an example of using float() with no argument:

number = float()
print (number)

Output:

0.0

In this tutorial, you've learned how to use the Python float() function. The float() function is a function to convert a number or string representing a number to a floating-point number.