Sometimes, we want to extract a floating number from a string with Python.
In this article, we’ll look at how to extract a floating number from a string with Python.
How to extract a floating number from a string with Python?
To extract a floating number from a string with Python, we call the re.findall
method.
For instance, we write
import re
d = re.findall("\d+\.\d+", "Current Level: 13.4db.")
to call re.findall
with a regex string to get the decimal numbers from the string.
Then d
is a list of numbers found in the string in the 2nd argument.
Conclusion
To extract a floating number from a string with Python, we call the re.findall
method.