Extract Numbers from String
Extract all integer numbers from a mixed string.
IntermediateTopic: String Programs
Python Extract Numbers from String Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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']
Understanding Extract Numbers from String
We use a regex \d+ to find all sequences of digits in the string.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.