Categories
Python Answers

How to extract a floating number from a string with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *