Exceptions & Files · Question 7 · Easy

io.StringIO lets a string be read like a text file. How many lines does readlines() return here?

import io

f = io.StringIO("red\ngreen\nblue\n")
print(len(f.readlines()))
  1. 4
  2. 1
  3. 3
  4. 15
Answer and explanation

Answer: C. 3

Explanation: readlines splits the content after every newline, giving 'red\n', 'green\n' and 'blue\n'. The final newline ends the third line; it does not start an empty fourth one.

Previous: The try block below completes without any error and the statement has an else… · Next: A try statement has except, else and finally clauses, and an error occurs in…

Practise exceptions & files

Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.

More exceptions & files questions

More python practice

Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge