How to Parse a String to a Float or Int in Python?

Spread the love

Problem –

You want to parse a string to a float or int in python.

Solution –

In [1]: string_num = "100.556"

In [2]: # convert to a float

In [3]: float(string_num)
Out[3]: 100.556

In [4]: # convert to an int

In [5]: int(float(string_num))
Out[5]: 100

Rating: 1 out of 5.

Leave a Reply