Bird
Raised Fist0

What will be the output of this code if the file 'example.txt' contains the text "Hello\nWorld"?

medium📝 Predict Output Q13 of Q15
Python - File Reading and Writing Strategies
What will be the output of this code if the file 'example.txt' contains the text "Hello\nWorld"?
with open('example.txt') as f:
    content = f.read()
print(content)
A"Hello\nWorld"
BHello\nWorld
CHello World
D['Hello', 'World']
Step-by-Step Solution
Solution:
  1. Step 1: Understand file content and read()

    The file contains two lines separated by a newline character. read() returns the full string including newline characters.
  2. Step 2: Print output interpretation

    When printed, the newline character \n creates a line break, so output shows as two lines: Hello and World.
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Newlines in file appear as line breaks when printed [OK]
Quick Trick: Printed newlines show as line breaks, not literal \n [OK]
Common Mistakes:
MISTAKES
  • Thinking print shows literal \n characters
  • Confusing string representation with printed output
  • Expecting a list instead of a string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes