Extract Numbers from String

Extract all integer numbers from a mixed string.

PythonIntermediate
Python
# Program to extract numbers from a string

import re

s = input("Enter a string: ")

numbers = re.findall(r"\d+", s)

print("Numbers found:", numbers)

Output

Enter a string: a12b3c45
Numbers found: ['12', '3', '45']

We use a regex \d+ to find all sequences of digits in the string.