Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Extract decimal numbers from a string in Python

How to extract decimal numbers from a string in python and can u explain this in detail and i need a code for this??


1 Answer
Pythonista

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']
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.