Write a program that accepts a sequence of words separated by whitespace as input to print the words composed of digits only

Question:

Write a program that accepts a sequence of words separated by whitespace as input to print the words composed of digits only.

Example:

If the following words are given as input to the program:

  • 2 cats and 3 dogs.
  • Then, the output of the program should be:
  • [‘2’, ‘3’]

In case of input data being supplied to the question, it should be assumed to be a console input.

Hints:

  • Use re.findall() to find all substrings using regex.

Solution:

import re
s = raw_input()
print re.findall("\d+",s)

Leave a Comment