ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(len('a\nb'), len('a\\nb'), len(r'a\nb'), len('a\rb'))
  1. A. 3 3 4 3
  2. B. 3 4 3 3
  3. C. 3 4 4 3
  4. D. 4 4 4 3

Correct answer: C. 3 4 4 3

'a\nb' has a real newline, so three characters. With double backslash the text holds a backslash and n, giving four. Raw strings keep the backslash, and \r is a carriage return, so 'a\rb' has three characters.

More Python Programming questions

Exams that ask this topic