ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
s = 'banana'
print(s.find('an', 2), s.rfind('an'), s.find('x'), s.index('na', 3))
  1. A. 3 3 -1 4
  2. B. 1 3 -1 2
  3. C. 3 3 -1 2
  4. D. 3 5 -1 4

Correct answer: A. 3 3 -1 4

'an' occurs at indices 1 and 3, so find from index 2 gives 3 and rfind gives 3. 'na' occurs at 2 and 4, so index('na', 3) gives 4. find returns -1 when the substring is absent.

More Python Programming questions

Exams that ask this topic