Sometimes, we want to parse a string to a float or int with Python.
In this article, we’ll look at how to parse a string to a float or int with Python.
How to parse a string to a float or int with Python?
To parse a string to a float or int with Python, we can use the float
or int
functions respectively.
For instance, we write:
a = "545.2222"
b = float(a)
c = int(b)
print(b)
print(c)
We call float
or int
with a
to convert a
to a float or int respectively.
Then we get:
545.2222
545
We have to use the int
function with a float.
But we can use float
with a number string.
Conclusion
To parse a string to a float or int with Python, we can use the float
or int
functions respectively.