How to convert a String type to Float or Int?

4

In Python, how can I convert a string "123.456" to a decimal number 123.456 ? And how a chain "32" to an integer 32 ?

    
asked by fedorqui 13.02.2017 в 14:40
source

1 answer

8

Use float() to convert to decimal e int() to convert to integer:

>>> a="123.456"
>>> float(a)
123.456
>>> int(float(a))
123

>>> b="32"
>>> int(b)
32
    
answered by 13.02.2017 / 14:40
source