You can apply the isdigit () function to each character in a string. Or you can use regular expressions.
Also I found How to find a single number in a string in Python? with very suitable ways to return numbers. The solution below is from the answer to this question.
number = re.search(r'\d+', yourString).group()
As an alternative:
number = filter(str.isdigit, yourString)
For more information, check out the regex document: http://docs.python.org/2/library/re.html.
Change: Returns real numbers, not a boolean, so the answers above are more true for your case.
The first method returns the first digit and subsequent consecutive digits. Thus, 1.56 will be returned as 1. 10000 will be returned as 10. 0207-100-1000 will be returned as 0207.
The second method does not work.
To extract all numbers, periods, and commas and not lose inconsistent numbers, use:
re.sub('[^\d.,]' , '', yourString)
Haini Nov 08 '13 at 12:44 2013-11-08 12:44
source share