How to Use float () Function in Python

# How to Use float () Function in Python

Vikram Singh
Assistant Manager - Content
Updated on Oct 30, 2023 13:40 IST

Float function is a built-in function in python that returns the floating-point number of a numeric value or a string representation of a numeric value. In this article, we will discuss how to use float function in python with the help of examples.

In any programming language, there are different data types that are stored in variables, so in Python. Data type in python is broadly divided into two different types: Mutable and Immutable.

In this article, we will discuss one of the types of python number functions, i.e., the float() function in Python.

Must Read: Data Types in Python

So, let’s dive deep to learn more about what is float function in Python and how to use them.

## What is the float () function in Python?

The float () function in Python returns a floating-point number of a number or a string representation of a numeric value. Floating point numbers are decimal numbers like 9.0, 45.2, etc.

### Syntax

` `
`float (value)Copy code`

Here, the parameter value is optional; it can be a regular integer or a string. Python float () function returns

1. a floating-point number.
2. If no value assigns it will return 0.0.
3. If string is passed without number, it will return an error.

#### Example-1: Convert integer value to float number.

` `
```#convert integer to decimal number float(2)Copy code```

Output

#### Example – 2: Convert string value to float number

` `
```#convert string number to decimal number float("2")Copy code```

Ouput

#### Example – 3: Assign no value tofloat function.

` `
```#no value assigned to the float function float()Copy code```

Output

#### Example – 4: Use float function with string.

` `
```#convert string (without having any number) to float float("infoEdge")Copy code```

Output

#### Example – 5: Use float function with infinity

` `
```# float() function with infinity print(float("inf"))print(float("InF"))print(float("InFiNiTy"))print(float("infinity"))Copy code```

Output

#### Example – 6: Use float function with NaN (Not a Number)

` `
```# float() function with NaN print(float("nan"))print(float("NaN"))Copy code```

Output

## Conclusion

In this article, we have discussed, how to use float function in python with the help of example.