How do I parse a string to a float or int?

Tristan Havelick picture Tristan Havelick · Dec 19, 2008 · Viewed 4.1M times · Source

In Python, how can I parse a numeric string like "545.2222" to its corresponding float value, 545.2222? Or parse the string "31" to an integer, 31?

I just want to know how to parse a float str to a float, and (separately) an int str to an int.

Answer

Harley Holcombe picture Harley Holcombe · Dec 19, 2008
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545