w3resource

PCEP Practice Test: Mastering Quotes and Apostrophes in Python Strings

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 "quotes and apostrophes inside 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 strings correctly includes both single and double quotes?

  1. text = 'She said, "It's raining."'
  2. text = 'She said, "It\'s raining."'
  3. text = "She said, "It's raining."
  4. text = 'She said, 'It's raining.'

Answer: B) text = 'She said, "It\'s raining."'

Explanation: Single quotes are used to enclose the string, allowing double quotes to be included directly. The single quote in "It's" is escaped using a backslash (\') to avoid ending the string early.

Question 2: Which of the following are valid ways to include both quotes and apostrophes in a string? (Choose all that apply)

  1. text = "It's \"Python\" time!"
  2. text = 'It\'s "Python" time!'
  3. text = "It's "Python" time!"
  4. text = 'It\'s \'Python\' time!'

Answer: A) text = "It's \"Python\" time!"
B) text = 'It\'s "Python" time!'
D) text = 'It\'s \'Python\' time!'

Explanation: Strings are immutable, meaning they cannot be modified in place. Concatenation and slicing create new strings.

Question 3: Complete the code to create a string that includes both single and double quotes: "It's a "Python" class!".

text = ______

Answer: 'It\'s a "Python" class!'

Explanation: The single quotes (' ') enclose the entire string. The apostrophe in It's is escaped with \'. The double quotes around "Python" are used directly without escaping because the string is enclosed in single quotes.

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

text = "She said, \"Python is fun!\""
print(text)
  1. She said, Python is fun!
  2. She said, \"Python is fun!\"
  3. She said, "Python is fun!"
  4. SyntaxError

Answer: C) She said, "Python is fun!"

Explanation: The backslashes escape the double quotes, so the string includes the quotes in the output.

Question 5: Insert the correct code to print the following string: I can't wait for the "Python" class!.

text = ______
print(text)

Answer: 'I can\'t wait for the "Python" class!'

Explanation: The string is enclosed in single quotes (' '). The apostrophe in "can't" is escaped with a backslash (\') to prevent it from ending the string early. The double quotes around "Python" are used directly because they are inside single quotes.

Question 6: Which of the following correctly includes a single quote in a double-quoted string?

  1. text = "It's a great day!"
  2. text = 'It\'s a great day!'
  3. text = 'It's a great day!'
  4. text = "It\'s a great day!"

Answer: A) text = "It's a great day!" D) text = "It\'s a great day!"

Explanation: In option A) the string is enclosed in double quotes, and the apostrophe in "It's" is allowed without escaping since it doesn't interfere with the double quotes and in option D) the string is enclosed in double quotes, and the apostrophe is unnecessarily escaped with a backslash. This works, but the escape is not needed in a double-quoted string.

Question 7: Which of the following strings are valid? (Choose all that apply)

  1. text = "He said, "Hello!""
  2. text = 'He said, "Hello!"'
  3. text = "He said, \"Hello!\""
  4. text = "He said, 'Hello!'"

Answer: B) text = 'He said, "Hello!"'
C) text = "He said, \"Hello!\""
D) text = "He said, 'Hello!'"

Explanation: Options B, C, and D are valid. Option A causes a syntax error because double quotes are not escaped or enclosed.

Question 8: Arrange the steps to correctly create a string that includes both single and double quotes.

  1. Assign the string to a variable.
  2. Choose single or double quotes to enclose the string.
  3. Escape any internal quotes as needed.

Answer: 2, 3, 1

Explanation: First, choose the appropriate quotes to enclose the string, then escape any internal quotes, and finally assign the string to a variable..

Question 9: Complete the code to create a string that includes the text He said, "It's Python time!".

text = ______

Answer: "He said, \"It's Python time!\""

Explanation: The string is enclosed in double quotes, and the internal double quotes are escaped.

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

text = 'It\'s "Python" class time!'
print(text)
  1. It's "Python" class time!
  2. It\'s "Python" class time!
  3. Its "Python" class time!
  4. SyntaxError

Answer: A) It's "Python" class time!

Explanation: The apostrophe is escaped, allowing it to be included in the single-quoted string.

Question 11: Insert the correct code to create a string that includes both quotes and apostrophes: She said, "It's time to learn Python!".

text = ______
print(text)

Answer: "She said, \"It's time to learn Python!\""

Explanation: Double quotes are used to enclose the string, and the internal double quotes are escaped.

Question 12: Which of the following statements about quotes in strings is true?

  1. Single quotes cannot be used to create strings in Python.
  2. Double quotes cannot be used to create strings in Python.
  3. Triple quotes can be used to create strings that span multiple lines.
  4. Only double quotes can include single quotes inside the string.

Answer: C) Triple quotes can be used to create strings that span multiple lines.

Explanation: Triple quotes (''' or """) can be used to create multi-line strings in Python.

Question 13: Which of the following are correct ways to include both quotes and apostrophes in a string? (Choose all that apply)

  1. text = "She said, "Python is fun!""
  2. text = 'She said, "Python\'s fun!"'
  3. text = "She said, \"Python's fun!\""
  4. text = 'She said, \'Python\'s fun!\''

Answer: B) text = 'She said, "Python\'s fun!"'
C) text = "She said, \"Python's fun!\""
D) text = 'She said, \'Python\'s fun!\''

Explanation: Options B, C, and D correctly escape quotes and apostrophes as needed. Option A causes a syntax error.

Question 14: Arrange the steps to correctly include an apostrophe in a string.

  1. Use double quotes to enclose the string.
  2. Define the string.
  3. Print the string.

Answer: 1, 2, 3

Explanation: First, use double quotes to enclose the string, then define the string, and finally print the string.

Question 15: Complete the code to create a string that includes a single quote: Don't worry about it!.

text = ______

Answer: "Don't worry about it!"

Explanation: Double quotes are used to enclose the string, allowing the single quote to be included directly.

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

text = 'She said, "It\'s a wonderful day!"'
print(text)
  1. She said, It's a wonderful day!
  2. She said, "It\'s a wonderful day!"
  3. She said, "It's a wonderful day!"
  4. SyntaxError

Answer: C) She said, "It's a wonderful day!"

Explanation: The single quote in "It's" is escaped, allowing the string to be valid and printed correctly.

17. Insert the correct code to include both single and double quotes in the string: The book is called "Python's Journey".

text = ______
print(text)

Answer: 'The book is called "Python\'s Journey"'

Explanation: Single quotes are used to enclose the string, and the apostrophe in "Python's" is escaped.

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

text = "It's called \"Python's Adventure\""
print(text)
  1. It's called "Python's Adventure"
  2. It\'s called "Python\'s Adventure"
  3. It's called Python's Adventure
  4. SyntaxError

Answer: A) It's called "Python's Adventure"

Explanation: The double quotes are escaped, allowing the string to include both single and double quotes.

Question 19: Which of the following strings will print correctly without causing an error? (Choose all that apply)

  • text = "The title is \"Python's Guide\""
  • text = 'It\'s a "Python" book'
  • text = "It's a "Python" book"
  • text = 'It\'s a \'Python\' book'

Answer: A) text = "The title is \"Python's Guide\""
B) text = 'It\'s a "Python" book'
D) text = 'It\'s a \'Python\' book'

Explanation: Options A, B, and D correctly handle both quotes and apostrophes. Option C causes a syntax error.

Question 20: Arrange the steps to correctly escape a double quote inside a string.

  1. Use double quotes to enclose the string.
  2. Print the string.
  3. Use a backslash to escape the internal double quote.

Answer: 1, 3, 2

Explanation: First, enclose the string in double quotes, then escape the internal double quote, and finally print the string.

Question 21: Complete the code to include a single quote in the string: It's a sunny day.

text = ______

Answer: "It's a sunny day"

Explanation: Double quotes are used to enclose the string, allowing the single quote to be included directly.

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

text = 'Python\'s "Best Practices"'
print(text)
  1. Python's Best Practices
  2. Python's "Best Practices"
  3. Python\'s "Best Practices"
  4. SyntaxError

Answer: B) Python's "Best Practices"

Explanation: The apostrophe is escaped, allowing the string to be valid and printed correctly.

Question 23: Insert the correct code to create a string that includes an apostrophe and double quotes: "It's a "must-read" book".

text = ______
print(text)

Answer: '"It\'s a "must-read" book"'

Explanation: Single quotes are used to enclose the string, and the apostrophe in "It's" is escaped.

Question 24: Which of the following is the correct way to create a string that includes a single quote inside a single-quoted string?

  1. text = 'It\'s a beautiful day!'
  2. text = "It's a beautiful day!"
  3. text = 'It's a beautiful day!'
  4. text = "It\'s a beautiful day!"

Answer: A) text = 'It\'s a beautiful day!'

Explanation: The apostrophe is escaped with a backslash, allowing it to be included inside a single-quoted string.

Question 25: Which of the following correctly include both single and double quotes in a string? (Choose all that apply)

  1. text = 'He said, "It\'s a good day!"'
  2. text = "He said, \"It's a good day!\""
  3. text = "He said, "It's a good day!"
  4. text = 'He said, 'It's a good day!''

Answer: A) text = 'He said, "It\'s a good day!"'
B) text = "He said, \"It's a good day!\""

Explanation: Options A and B correctly escape the necessary quotes to include both single and double quotes in the string. Options C and D will raise syntax errors.



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-quotes-and-apostrophes-inside-strings.php