ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print('aaaa'.count('aa'), 'aaaa'.replace('aa', 'b'), len('aaaaa'.split('aa')))
  1. A. 2 b 3
  2. B. 2 bb 3
  3. C. 3 bb 3
  4. D. 2 bb 2

Correct answer: B. 2 bb 3

str.count and replace work on non-overlapping matches, so 'aaaa' has 2 matches of 'aa'. 'aaaaa'.split('aa') gives ['', '', 'a'], which has length 3.

More Python Programming questions

Exams that ask this topic