In this guide, we are going to learn all about the Python float() function to convert any number to a float number. Using the python float method you can convert any integer as well as any string number to a floating pointing number.
Headings of Contents
Python float() Function
The float() function in Python is a built-in function that is used to convert any specified value into a floating pointing number.
Syntax
The syntax of the float function in python is:-
float(value)
float() parameter
float() function take a single parameter.
- value:- A number or a string that can be converted into a floating point number.
Return Value
The return value of the float function in Python is always a float number.
Python float example
let’s see the float() function along with examples. Here will try to convert different-different numbers into floating-point numbers.
Example: Converting integer value to float
Convert integer to floating point.
a = 12
x = float(a)
print(x)
The output will be:- 12.0
Example: Convert string float number to valid floating pointing numbers
Convert string number to floating point number.
b = '12.88'
y = float(b)
print(y)
The output will be:- 12.88
Example: Converting a list of integers into a float
Convert a list of numbers into float numbers.
num = [12, 20, 12, '12', 60, 78, '60.5']
for i in num:
print(float(i))
Output
12.0
20.0
12.0
12.0
60.0
78.0
60.5
Conclusion
In this tutorial, you have learned all about the float() function in Python to convert any number entered by users to floating-point numbers. Using the float function in Python, you can convert any string number to a floating-point number as well.
If this article helped you, please keep visiting for further python built-in functions tutorials.
Other Python built-in functions
For more information:- Click Here