Python float() Function



The Python float() function is used to convert the given value into a a floating-point number. A floating-point number is a numerical type that represents real numbers with decimal points. It can convert various types of data, such as numeric strings or integer numbers, into float.

Note that the provided string should represent a valid numeric value; otherwise, a ValueError will be raised.

Syntax

Following is the syntax of Python float() function −

float(x)

Parameters

This function accepts a numerical value or a string containing a numerical representation as its parameter.

Return Value

This function returns a float object.

Example

Following is an example of the Python float() function. Here, we are converting the integer "42" to a floating-point number −

num = 42
float_num = float(num)
print('The float value obtained is:',float_num)

Output

Following is the output of the above code −

The float value obtained is: 42.0

Example

Here, we are converting the string "3.14" to a float, resulting in the floating-point number 3.14 −

str_num = "3.14"
float_num = float(str_num)
print('The float value obtained is:',float_num)

Output

Output of the above code is as follows −

The float value obtained is: 3.14

Example

Now, we are using the float() function to convert the result of a mathematical expression into a floating-point number. Here, the result of the expression "10 / 3" is converted to a float −

num1 = 10
num2 = 3
expression_result = float(num1 / num2)
print('The float value obtained is:',expression_result)

Output

The result obtained is as shown below −

The float value obtained is: 3.3333333333333335

Example

If you pass a string containing non-numeric characters to the float() function, it will raise a ValueError.

Here, we are converting the string "25 bananas" into a float −

# Example with Error
mixed_string = "25 bananas"
number_part = float(mixed_string)
print(number_part)

Output

We can see in the output below that an issue arises because the string contains non-numeric characters (' apples'), causing a ValueError during the conversion −

Traceback (most recent call last):
  File "C:\Users\Lenovo\Desktop\untitled.py", line 3, in <module>
    number_part = float(mixed_string)
ValueError: could not convert string to float: '25 bananas'

Example

Now, we handle a string containing both numeric and non-numeric characters.

First, we extract only the numeric characters from the "mixed_string." We create the "numeric_part" variable using a list comprehension that filters out non-numeric characters, resulting in a string with only digits "25". Finally, we use the float() function to convert this string into a floating-point number −

# Example without Error
mixed_string = "25 bananas"
numeric_part = ''.join(char for char in mixed_string if char.isdigit())
number_part = int(numeric_part)
print('The integer value obtained is:',number_part)

Output

The result produced is as follows −

The float value obtained is: 25.0
python-float-function.htm
Advertisements