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

pythonfloat(number or string)

Example

Let's use the float function to turn a string into a floating point number:

pythontxt = '123'
num = float(txt)
print(num)

Result of code execution:

python123.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:

pythontxt = '12.35'
num = float(txt)
print(num)

Result of code execution:

python12.35

Example

Now let's pass a number to the float function parameter:

pythonnum1 = 12
num2 = float(num1)
print(num2)

Result of code execution:

python12.0

See also

  • function int,
    which creates the number
  • function str,
    which creates a string
  • function list,
    which creates a list