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 browser
1# Program to extract numbers from a string
2
3import re
4
5s = input("Enter a string: ")
6
7numbers = re.findall(r"\d+", s)
8
9print("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.