The float function
The float function creates a new floating-point number from the original object. In the parameter, we specify the object from which we will make a floating-point number.
Syntax
float(number or string)
Example
Let's use the float function to turn a string into a floating point number:
txt = '123'
num = float(txt)
print(num)
Result of code execution:
123.0
Example
Let's pass a string to the float function, in which the digits are separated by a period. After conversion, we get the corresponding fraction:
txt = '12.35'
num = float(txt)
print(num)
Result of code execution:
12.35
Example
Now let's pass a number to the float function parameter:
num1 = 12
num2 = float(num1)
print(num2)
Result of code execution:
12.0