Python String Basics Quiz

Quiz 5 12 questions

Prerequisites for the quiz

  1. Python String Methods

Are you ready?

12 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.
In Python, string indexes start at 1. True or false?
In most programming languages, string indexes start at 0, and Python is no exception. For more details, please refer to Python String Basics.
In the following code, 'Hello' is a string literal. True or false?
s = 'Hello'
When a string is denoted using quotation marks, it's termed as a string literal; as in the code above. Hence, the correct choice is (A).
The two strings 'Hello' and "Hello" are identical to each other. Yes or no?
The strings 'Hello' and "Hello" are defined in different ways — one using single quotes and the other using double quotes — however they denote exactly the same piece of information. In other words, they are identical to each other.
Is there a problem in the code below? If yes, then how can we rectify it?
s = 'Python's call!'
The problem is that the string s, which is defined using ' (single quotes), contains a single quote itself. This can be rectified by either escaping this quote character using a backslash or by simply using a multiline string. Hence, the correct choice is (D).

For more specific details, please refer to Python String Basics — escaping characters and Python String Basics — multiline strings.
How to denote a multiline string in Python?
A multiline string can be denoted by a pair of triple-single-quotes ('''Hello World!''') or triple-double-quotes ("""Hello World!"""). Hence, the correct choice here is (C). For more details on multiline strings, refer to Python String Basics — multiline strings.
How to access the second character of a string s?
From a string s, a given character can be accessed via bracket notation — putting the index of the desired character within the pair of brackets. To access the second character, for example, we'll write s[1]. Hence, the correct choice is (C).
What does
len(' Hello')
return?
When given a string, the len() function in Python returns the numbers of characters in it. In this case, it returns 6, and this goes with choice (B).
How to access the last character of a string s?
The last character of a string s can be accessed directly using s[-1], or by using s[len(s) - 1], realising the fact that the last character is at an index one lesser than the length of the string. This goes with choice (D).
What does the following code print?
s = 'Hello'
print(s[1:3])
The expression s[1:3] slices the string s from index 1 exactly upto index 2 (not 3, since it is exclusive), to yield the substring 'el'. The correct choice is, therefore, (A). Read more at Python String Basics — slicing a string.
When used on strings, what does the * operator do?
The * operator, when used on string, replicates the strings a given number of times. Read more details at Python String Basics — string replication.
What is the string r'\Desktop\groceries.txt' called?
Strings that are preceded by an r are commonly known as raw strings. They prevent the need to escape backslashes — just write them, as is. To learn more about them, refer to Python String Basics — raw strings.
Which of the following strings does r'Hello\\World' evaluate down to?
For more details, please refer to Python String Basics — raw strings.

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage