w3resource

PCEP Practice Test: Understanding Multi-line Strings in Python

PCEP Certification Practice Test - Questions, Answers and Explanations

Below is a set of questions for the Certified Entry-Level Python Programmer (PCEP) examination focusing on the subtopic "multi-line strings." The questions use various formats, including single and multiple-select questions, fill-in-the-gap, code fill, code insertion, sorting, and more.

Question 1: Which of the following is the correct way to create a multi-line string in Python?

  1. text = 'This is a\nmulti-line string'
  2. text = """This is a multi-line string"""
  3. text = 'This is a' + '\n' + 'multi-line string'
  4. text = "This is a \multi-line string"

Answer: B) text = """This is a multi-line string"""

Explanation: Triple quotes (""" or ''') are used to create multi-line strings in Python, allowing the string to span multiple lines.

Question 2: Which of the following methods can be used to create a multi-line string in Python? (Choose all that apply)

  • text = """First line\nSecond line"""
  • text = '''First line\nSecond line'''
  • text = 'First line' + '\n' + 'Second line'
  • text = "First line \n Second line"

Answer: A) text = '''First line\nSecond line'''
B) text = """First line\nSecond line"""
C) text = 'First line' + '\n' + 'Second line'
D) text = "First line \n Second line"

Explanation: All the options correctly create multi-line strings either using triple quotes or by explicitly adding the newline character (\n).

Question 3: Complete the code to create a multi-line string using triple quotes.

multi_line = ______

Answer: '''This is a multi-line string'''

Explanation: Triple quotes (''' or """) allow for a string that spans multiple lines.

Question 4: What will be the output of the following code?

text = '''Line 1
Line 2
Line 3'''
print(text)
  1. Line 1Line 2Line 3
  2. Line 1 Line 2 Line3
  3. Line 1\nLine 2\nLine 3
  4. Line 1 Line 2 Line 3

Answer: D) Line 1 Line 2 Line 3

Explanation: The triple-quoted string spans multiple lines, and Python automatically includes line breaks between each line of text.

Question 5: Insert the correct code to create a string poem that contains the following text:

Roses are red,
Violets are blue,
Python is fun,
And so are you.
 
poem = ______
print(poem)

Answer: '''Roses are red, Violets are blue, Python is fun, And so are you.'''

Explanation: Triple quotes are used to create a multi-line string, preserving the format of the text.

Question 6: Which of the following is the correct way to store a paragraph as a multi-line string in Python?

  1. paragraph = """This is line 1.\nThis is line 2.\nThis is line 3."""
  2. paragraph = 'This is line 1.\nThis is line 2.\nThis is line 3.'
  3. paragraph = '''This is line 1. This is line 2. This is line 3.'''
  4. paragraph = "This is line 1. \n This is line 2. \n This is line 3."

Answer: C) paragraph = '''This is line 1. This is line 2. This is line 3.'''

Explanation: Triple quotes allow you to write a multi-line string directly across multiple lines.

Question 7: Which of the following are valid ways to include a new line in a multi-line string? (Choose all that apply)

  1. Using triple quotes (''' or """)
  2. Using the newline character (\n)
  3. Using string concatenation (+) with \n
  4. Using a tab character (\t)

Answer: A) Using triple quotes (''' or """)
B) Using the newline character (\n)
C) Using string concatenation (+) with \n

Explanation: Triple quotes and the newline character (\n) are commonly used to include new lines in a multi-line string. String concatenation can also be used to add \n between strings.

Question 8: Arrange the steps to correctly create a multi-line string and print it.

  1. Use triple quotes to define the multi-line string.
  2. Print the variable.
  3. Assign the string to a variable.

Answer: 1, 3, 2

Explanation: First, define the multi-line string using triple quotes, then assign it to a variable, and finally print the variable.

Question 9: Complete the code to create a multi-line string with a newline character between each line.

text = "Line 1" + ______ + "Line 2" + ______ + "Line 3"
print(text)

Answer: "\n", "\n"

Explanation: The \n escape character adds a newline between each line.

Question 10: What will be the output of the following code?

text = '''Hello,
World!
Welcome to Python!'''
print(text)
  1. Hello, World! Welcometo Python!
  2. Hello, World! Welcome to Python!
  3. Hello,\nWorld!\nWelcome to Python!
  4. Hello, \n World! \n Welcome to Python!

Answer: B) Hello, World! Welcome to Python!

Explanation: Triple quotes allow the text to span multiple lines, preserving the line breaks as they are written.

Question 11: Insert the correct code to define a multi-line string greeting containing the text:

Hi,
How are you?
Have a great day!
 
greeting = ______
print(greeting)

Answer: '''Hi, \nHow are you? \nHave a great day!'''

Explanation: Triple quotes are used to create the multi-line string and preserve the format with line breaks.

Question 12: What is the result of the following code?

message = """Python is great!
It's also fun!
Let's learn Python!"""
print(message)
  1. Python is great! It's also fun! Let's learn Python!
  2. Python is great!\nIt's also fun!\nLet's learn Python!
  3. Python is great! It's also fun! Let's learn Python!
  4. SyntaxError

Answer: C)
Python is great!
It's also fun!
Let's learn Python!

Explanation: Triple quotes allow the string to be written over multiple lines with line breaks preserved in the output.

Question 13: Which of the following are benefits of using triple quotes for multi-line strings? (Choose all that apply)

  1. They allow strings to span multiple lines without using \n.
  2. They make the code more readable when dealing with large blocks of text.
  3. They eliminate the need to concatenate multiple strings together.
  4. They automatically format the string with indentation.

Answer: A) They allow strings to span multiple lines without using \n.
B) They make the code more readable when dealing with large blocks of text.
C) They eliminate the need to concatenate multiple strings together.

Explanation: Triple quotes allow for multi-line strings without needing explicit newline characters or concatenation, improving readability and ease of use.

Question 14: Arrange the steps to correctly create a string that prints a multi-line message.

  1. Print the variable.
  2. Use triple quotes to create the string.
  3. Assign the string to a variable message.

Answer: 2, 3, 1

Explanation: First, create the string using triple quotes, then assign it to the message variable, and finally print it.

Question 15: Complete the code to create a multi-line string without using triple quotes.

text = "Line 1" + ______ + "Line 2" + ______ + "Line 3"
print(text)

Answer: "\n", "\n"

Explanation: The \n escape sequence is used to create new lines without using triple quotes.

Question 16: What will be the output of the following code?

text = '''This is line 1.
This is line 2.
This is line 3.'''
print(text)
  1. This is line 1. This is line 2. This is line 3.
  2. This is line 1. This is line 2. This is line 3.
  3. This is line 1. This is line 2. This is line 3.
  4. This is line 1.\nThis is line 2.\nThis is line 3.

Answer: A)
This is line 1.
This is line 2.
This is line 3.

Explanation: Triple quotes preserve the line breaks and print the string over multiple lines.

17. Insert the correct code to define a multi-line string that includes a blank line between two lines of text.

text = ______
print(text)

Answer: '''Line 1
[empty line]
Line 2'''

Explanation: Triple quotes allow for blank lines within a multi-line string by simply adding an empty line between the text.

Question 18: What will be the result of the following code?

text = '''First line
Second line
Third line'''
print(text)
  1. First line Second line Third line
  2. First line\nSecond line\nThird line
  3. First line Second line Third line
  4. None

Answer: C)
First line
Second line
Third line

Explanation: Triple quotes preserve the formatting of the multi-line string with line breaks included.

Question 19: Which of the following are correct ways to create a multi-line string? (Choose all that apply)

  • text = '''This is a multi-line string'''
  • text = """This is a multi-line string"""
  • text = 'This is a\nmulti-line\nstring'
  • text = "This is a\nmulti-line\nstring"

Answer: A) text = '''This is a multi-line string'''
B) text = """This is a multi-line string"""
C) text = 'This is a\nmulti-line\nstring'
D) text = "This is a\nmulti-line\nstring"

Explanation: All options correctly create multi-line strings, either using triple quotes or explicit newline characters.

Question 20: Arrange the steps to correctly format a multi-line string that contains a poem.

  1. Print the poem.
  2. Assign the poem to a variable using triple quotes.
  3. Include line breaks where necessary.

Answer: 2, 3, 1

Explanation: First, use triple quotes to create the poem, add line breaks, and then print the poem.

Question 21: Complete the code to create a multi-line string that starts with "Hello" on one line and "World!" on the next.

greeting = ______ + '\n' + ______
print(greeting)

Answer: "Hello", "World!"

Explanation: The \n escape character is used to insert a newline between the words "Hello" and "World!".

Question 22: What will be the output of the following code?

text = """Line one
Line two
Line three"""
print(text)
  1. Line one Line two Line three
  2. Line one Line two Line three
  3. Line one\nLine two\nLine three
  4. None

Answer: B)
Line one
Line two
Line three

Explanation: The string created using triple quotes preserves the line breaks.

Question 23: Insert the correct code to define a multi-line string story that contains the following text:

Once upon a time,
In a land far away,
There lived a Python coder.
 
story = ______
print(story)

Answer:
'''Once upon a time,
In a land far away,
There lived a Python coder.'''

Explanation: Triple quotes allow the string to span multiple lines with line breaks included.

Question 24: Which of the following statements about multi-line strings in Python is true?

  1. Multi-line strings must be created using double quotes.
  2. Triple quotes allow strings to span multiple lines.
  3. The \n character cannot be used inside triple quotes.
  4. Multi-line strings automatically convert line breaks to spaces.

Answer: B) Triple quotes allow strings to span multiple lines.

Explanation: Triple quotes (''' or """) are specifically designed for multi-line strings, preserving the format and line breaks.

Question 25: Which of the following correctly create a multi-line string that includes a blank line between text? (Choose all that apply)

  1. text = '''Line 1\n\nLine 2'''
  2. text = """Line 1\n\nLine 2"""
  3. text = "Line 1\n\nLine 2"
  4. text = '''Line 1\nLine 2'''

Answer: A) text = '''Line 1\n\nLine 2'''
B) text = """Line 1\n\nLine 2"""
C) text = "Line 1\n\nLine 2"

Explanation: All options correctly create multi-line strings with a blank line between "Line 1" and "Line 2", either using triple quotes or explicit newline characters.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python/certificate/data-collections-strings-multi-line-strings.php