Python String Methods Quiz

Quiz 6 8 questions

Prerequisites for the quiz

  1. Python String Methods

Are you ready?

8 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What does the upper() method do?
The upper() method uppercases all characters in a given string, and returns the resulting string back. See more details at Python String Methods — lower and upper casing.
Which of following strings does
"Two days to go!".title()
evaluate down to?
The title() method capitalises each word in a given test string. The expression "Two days to go!".title() therefore returns "Two Days To Go", which goes with choice (B).

See more details at Python String Methods — title casing.
What does the following code print?
s = 'HELLO'
print(s.isupper())
The isupper() method returns True if the given string has all uppercase characters. Here, since s is indeed in upper case, s.isupper() returns True. This goes with choice (A).

See more details at Python String Methods — title casing.
How to remove the extra whitespace at the end of the string s shown below?
s = 'Hello World     '
Whitespace at the ends of a string can be removed using the strip() method (or using lstrip() or rstrip() separately).

See more details at Python String Methods — striping characters.
Which of the following expressions turns the string s = '10 20 30' into the list ['10', '20', '30']?
The split() method, called without an argument, breaks apart a string into a list of substrings, at each sequence of whitespace characters.

For more info, please refer to Python String Methods — splitting into substrings.
How to obtain the string '10, 20, 30' from the list l = ['10', '20', '30']?
Python lists have no join() method on them. To join items of a list into a string, we have to instead use the string method join() on the separator string. The separator in this case is ', ', hence the correct choice is ', '.join(l).

For more info, please refer to Python String Methods — joining into string.
What does the following code print?
s = 'Eggs, spam and eggs'
print(s.count('eggs'))
The count() method counts all the occurences of the given argument in the main string, and returns back this count, as an integer. It operates case-sensitively. In the string s above, 'eggs' occurs once, so we get 1 printed.

For more info, please refer to Python String Methods — counting substrings.
What does the following code print?
s = 'Spam, eggs, spam and eggs.'
print(s.find('eggs'))
find() returns the index of the first occurence of its argument in the main string, or otherwise -1. In the string s above, 'eggs' first occurs at index 6, likewise we get 6 printed.

For more info on find(), please refer to Python String Methods — finding substrings.