Python float()

Python float() builtin function creates and returns a floating point number from the given data.

In this tutorial, we will learn about the syntax of Python float() function, and learn how to use this function with the help of examples.

Syntax

The syntax of float() function is

float([x])

where

ParameterRequired/OptionalDescription
xOptionalAn integer / A string object / A floating point number

If x is string, then float() attempts to convert the given string into floating point number. If float() fails to covert the given string into float object, then float() throws ValueError.

If x is integer, then float() convert the given integer into floating point number.

If x is another floating point number, then float() creates a new float object with value equal to the given float number.

If no argument is given to float(), then it returns a value of 0.0.

Returns

The function returns object of type float.

ADVERTISEMENT

Examples

1. float(‘2.23’)

In this example, we will pass a string '2.23' as argument to float() function. The function converts the string to a floating point number.

Python Program

x = '2.23'
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Return value : 2.23

If the input string is not a valid floating point value, then float() throws ValueError.

In the following program, the string contains a character that is not allowed in a floating point number.

Python Program

x = '2a23'
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Traceback (most recent call last):
  File "d:/workspace/python/example.py", line 2, in <module>
    result = float(x)
ValueError: could not convert string to float: '2a23'

2. float(integer)

In this example, we will pass an integer 5 to float() function. float() function returns a float object whose value is created from the given integer.

Python Program

x = 5
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Return value : 5.0

3. float() – No Argument

If we do not pass any argument to float(). In such case, float() function returns a value of 0.0.

Python Program

result = float()
print(f'Return value : {result}')
Try Online

Output

Return value : 0.0

4. float(float)

If we pass a float object to float() function, the function returns a new float object with the value equal to that of the given float object.

Python Program

x = 2.23
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Return value : 2.23

5. float() – Positive Infinity

We can pass values ‘inf’ of ‘infinity’ (case insensitive) to float() function as argument to create a float object with value of infinity inf.

Python Program

x = 'Infinity'
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Return value : inf

6. float() – Negative Infinity

We can pass values ‘-inf’ of ‘-infinity’ (case insensitive) to float() function as argument to create a float object with value of negative infinity -inf.

Python Program

x = '-Infinity'
result = float(x)
print(f'Return value : {result}')
Try Online

Output

Return value : -inf

Conclusion

In this Python Tutorial, we have learnt the syntax of Python float() builtin function, and also learned how to use this function, with the help of Python example programs.