Extract Numbers from String in Python
Extract all integer numbers from a mixed string.
IntermediateString ProgramsExample 21 of 25
extract-numbers-from-string.py
Run in browser1# Program to extract numbers from a string23import re45s = input("Enter a string: ")67numbers = re.findall(r"\d+", s)89print("Numbers found:", numbers)
Output
Enter a string: a12b3c45 Numbers found: ['12', '3', '45']
What's going on
We use a regex \d+ to find all sequences of digits in the string.