This answer key contains 4 multiple-choice, 4 fill-in-the-blank, and 4 output-prediction questions for Python String Methods, with the exact answer and a short explanation for every question.
The answers are shown in full, so use this page after making an attempt. The practice page is the better first pass.
What does strip() remove from a string?
Answer: Whitespace from both ends
Why: strip() removes leading and trailing whitespace by default.
What is the result of "Latte".lower()?
Answer: latte
Why: lower() returns a copy with all letters in lowercase.
What does "a,b,c".split(",") return?
Answer: A list of three strings
Why: split returns a list containing the fields between separators.
What is the result of "-".join(["a", "b"])?
Answer: a-b
Why: join places the separator string between each item.
Complete the method that removes spaces from both ends.
text = " latte "
print(text.____())Answer: strip
Why: strip() removes leading and trailing whitespace.
Complete the method so PYTHON becomes python.
print("PYTHON".____())Answer: lower
Why: lower() returns a lowercase copy of the string.
Complete the method that splits the text at each comma.
print("a,b".____(","))Answer: split
Why: split(separator) breaks a string into a list.
Complete the method that joins the two words with a space.
print(" ".____(["hello", "world"]))Answer: join
Why: The separator string calls join with an iterable of strings.
What appears on the screen?
print(" latte ".strip())Answer: latte
Why: strip removes the spaces at both ends.
What is printed?
print("Python".lower())Answer: python
Why: lower converts every letter to lowercase.
What is the output?
print("a,b,c".split(","))Answer: ['a', 'b', 'c']
Why: split returns a list of the three fields.
What does this code print?
print("-".join(["a", "b", "c"]))Answer: a-b-c
Why: join inserts a hyphen between each item.
Try the questions first
Open the practice page for Python String Methods and answer the same 12 questions with instant feedback.