ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import io
s = io.StringIO("abc\n")
print(repr(s.readline()), repr(s.readline()))
  1. A. 'abc' ''
  2. B. 'abc\n' '\n'
  3. C. 'abc' 'abc'
  4. D. 'abc\n' ''

Correct answer: D. 'abc\n' ''

readline keeps the trailing newline, so the first call returns 'abc\n'. At end of input readline returns an empty string, which is what a while-loop over input must handle.

More Python Programming questions

Exams that ask this topic