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?
- text = 'This is a\nmulti-line string'
- text = """This is a multi-line string"""
- text = 'This is a' + '\n' + 'multi-line string'
- 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)
- Line 1Line 2Line 3
- Line 1 Line 2 Line3
- Line 1\nLine 2\nLine 3
- 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?
- paragraph = """This is line 1.\nThis is line 2.\nThis is line 3."""
- paragraph = 'This is line 1.\nThis is line 2.\nThis is line 3.'
- paragraph = '''This is line 1. This is line 2. This is line 3.'''
- 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)
- Using triple quotes (''' or """)
- Using the newline character (\n)
- Using string concatenation (+) with \n
- 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.
- Use triple quotes to define the multi-line string.
- Print the variable.
- 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)
- Hello, World! Welcometo Python!
- Hello, World! Welcome to Python!
- Hello,\nWorld!\nWelcome to Python!
- 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)
- Python is great! It's also fun! Let's learn Python!
- Python is great!\nIt's also fun!\nLet's learn Python!
- Python is great! It's also fun! Let's learn Python!
- 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)
- They allow strings to span multiple lines without using \n.
- They make the code more readable when dealing with large blocks of text.
- They eliminate the need to concatenate multiple strings together.
- 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.
- Print the variable.
- Use triple quotes to create the string.
- 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)
- This is line 1. This is line 2. This is line 3.
- This is line 1. This is line 2. This is line 3.
- This is line 1. This is line 2. This is line 3.
- 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)
- First line Second line Third line
- First line\nSecond line\nThird line
- First line Second line Third line
- 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.
- Print the poem.
- Assign the poem to a variable using triple quotes.
- 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)
- Line one Line two Line three
- Line one Line two Line three
- Line one\nLine two\nLine three
- 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?
- Multi-line strings must be created using double quotes.
- Triple quotes allow strings to span multiple lines.
- The \n character cannot be used inside triple quotes.
- 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)
- text = '''Line 1\n\nLine 2'''
- text = """Line 1\n\nLine 2"""
- text = "Line 1\n\nLine 2"
- 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.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics