python – How to convert a String type to Float or Int?

Question:

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

Answer:

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

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

>>> b="32"
>>> int(b)
32
Scroll to Top