Extract decimal numbers from a string in Python

PythonServer Side ProgrammingProgramming

Using RegEx module is the fastest way.

>>> import re

Assuming that the string contains integer and floating point numbers as well as below −

>>> s='my age is 25. I have 55.50 percent marks and 9764135408 is my number'

the findall() function returns a list of numbers matching given pattern which includes digit before and after decimal point

>>> re.findall('\d*\.?\d+',s)

Result is a list object of all numbers

['25', '55.50', '9764135408']
Published on 08-May-2018 14:50:30