[SOLVED] Extract Number from String in Python


I am new to Python and I have a String, I want to extract the numbers from the string. For example:

str1 = "3158 reviews"
print (re.findall('\d+', str1 ))

Output is ['4', '3']

I want to get 3158 only, as an Integer preferably, not as List.


You can filter the string by digits using str.isdigit method,

>>> int(filter(str.isdigit, str1))

Answered By – Vishnu Upadhyay

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

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