In this example, I have shared how to find all the numbers in a string using regular expression in Python. The parameters to these function are the pattern which we want to extract and the string from which we want to extract. Please note with below example we get only the digits and not the decimal points or the negative signs.
1 2 3 4 5 |
import re str=input("Enter a String with numbers: \n") #Create a list to hold the numbers num_list = re.findall(r'\d+', str) print(num_list) |
Program Output
1 2 3 |
Enter a String with numbers: Go to 13.8 miles and then -4.112 miles. ['13', '8', '4', '112'] |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.