Which of these code snippets correctly prints multiple lines using a multi-line string?
easy📝 Syntax Q12 of 15
Python - File Reading and Writing Strategies
Which of these code snippets correctly prints multiple lines using a multi-line string?
Aprint('''Line 1\nLine 2\nLine 3''')
Bprint('Line 1\n' + 'Line 2\n' + 'Line 3')
Cprint('Line 1\nLine 2\nLine 3')
Dprint('''Line 1
Line 2
Line 3''')
Step-by-Step Solution
Solution:
Step 1: Understand how multi-line strings handle newlines
Triple-quoted strings keep line breaks as typed, so actual newlines appear without \n escape sequences.
Step 2: Analyze each option
print('''Line 1
Line 2
Line 3''') uses triple quotes with actual newlines inside, so it prints three lines. print('''Line 1\nLine 2\nLine 3''') uses \n inside triple quotes, which prints literal \n, not new lines. Options B and D use single quotes with \n, which print new lines correctly, but are not multi-line strings.
Final Answer:
print('''Line 1
Line 2
Line 3''') -> Option D
Quick Check:
Triple quotes with real newlines print multiple lines [OK]
Quick Trick:Triple quotes keep line breaks as typed, no need for \n [OK]
Common Mistakes:
Using \n inside triple quotes expecting line breaks
Confusing escape sequences with actual newlines
Using single quotes for multi-line strings
Master "File Reading and Writing Strategies" in Python
9 interactive learning modes - each teaches the same concept differently