ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
s = 'a1b22c333'
import re
print(re.findall(r'\d+', s), sum(int(x) for x in re.findall(r'\d+', s)))
  1. A. ['1', '22', '333'] 1
  2. B. ['a1', 'b22', 'c333'] 356
  3. C. ['1', '22', '333'] 356
  4. D. ['1', '2', '2', '3', '3', '3'] 14

Correct answer: C. ['1', '22', '333'] 356

re.findall with \d+ collects runs of digits as strings. Adding the numbers 1, 22 and 333 gives 356.

More Python Programming questions

Exams that ask this topic