Python Practice › String Programs › Program 83

Extract numbers as integers

EasyString Programs · 83 of 106

Write a Python program to extract the numbers from a string.

Advertisement
Input
"python 456 self learning 89"
Output
[456, 89]

Approach

  1. Keep words that are digits and convert them.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment