Python Practice › String Programs › Program 83
Extract numbers as integers
Write a Python program to extract the numbers from a string.
Input
"python 456 self learning 89"
Output
[456, 89]
Approach
- Keep words that are digits and convert them.
Solution
text = "python 456 self learning 89"print([int(w) for w in text.split() if w.isdigit()])Output
[456, 89]
Learn the concept
New to this topic? Read the lesson, then come back.