5 of 151 menu

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

See also

  • function int,
    which creates the number
  • function str,
    which creates a string
  • function list,
    which creates a list
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline