# How to Use the Python float() Function

09/12/2021

Contents

In this article, you will learn how to use the Python float() function.

## Python float() Function

The float() function in Python is used to convert a given number or a string that represents a number to a floating-point number.

##### Syntax:

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

`float([x])`

##### Parameters:

`x`

is the number or string that you want to convert to a floating-point number. The float() function returns the floating-point representation of the input number or string.
##### Example:

Here are some examples of how to use the float() function in Python:

###### Convert an integer to a floating-point number:

```
num_int = 10
num_float = float(num_int)
print(num_float) # Output: 10.0
```

###### Convert a string to a floating-point number:

```
num_str = "3.14"
num_float = float(num_str)
print(num_float) # Output: 3.14
```

###### Convert a string with scientific notation to a floating-point number:

```
num_str = "1.23e-4"
num_float = float(num_str)
print(num_float) # Output: 0.000123
```

It is important to note that the float() function can raise a ValueError exception if the input string is not a valid representation of a floating-point number. For example:

```
num_str = "abc"
num_float = float(num_str) # Raises ValueError: could not convert string to float: 'abc'
```

So, be sure to handle exceptions appropriately if you are using the float() function with user input.