Question Detail

COS 102 Python strings Objective Question

Question

Which of these lines of code correctly defines a multi-line string in Python?
Options
A <code>my_string = 'Line 1 Line 2'</code>
B <code>my_string = """Line 1 Line 2"""</code>
C <code>my_string = "Line 1 Line 2"</code>
D All of the above
Correct Answer
Correct Answer

Option D is the correct answer.

Detailed Explanation

While triple quotes ("""""" or ''' ''') are explicitly mentioned for 'Multi-line' strings, single and double quotes can also define multi-line strings by explicitly including newline characters (\n). Therefore, all options are valid ways to define a string with multiple lines.

Hint

Although triple quotes are designed for multi-line strings, `\n` can be used within single or double quotes to achieve the same effect.

Question Info