Question Detail

COS 102 Python strings Objective Question

Question

Which of the following Python statements correctly creates a multi-line string?
Options
A <code>my_string = 'Line1\nLine2'</code>
B <code>my_string = """Line1\nLine2"""</code>
Correct Answer
C <code>my_string = "Line1" + "Line2"</code>
D <code>my_string = 'Line1', 'Line2'</code>
Correct Answer

Option B is the correct answer.

Detailed Explanation

Option B uses triple quotes, which are specifically designed in Python to enclose multi-line strings. The \n character in Option A would create a new line, but the question asks about the method of creating a multi-line string directly via quotes. Option C is concatenation, not multi-line string creation. Option D creates a tuple of strings.

Hint

Recall the three main ways Python offers to enclose text, especially for text spanning multiple lines.

Question Info