22. Quiz: String Methods Practice

Quiz: String Methods Coding Practice

Below, we have a string variable that contains the first verse of the poem, If by Rudyard Kipling. Remember, \n is a special sequence of characters that causes a line break (a new line).

verse = "If you can keep your head when all about you\n  Are losing theirs and blaming it on you,\nIf you can trust yourself when all men doubt you,\n  But make allowance for their doubting too;\nIf you can wait and not be tired by waiting,\n  Or being lied about, don’t deal in lies,\nOr being hated, don’t give way to hating,\n  And yet don’t look too good, nor talk too wise:"

Use the code editor below to answer the following questions about verse and use Test Run to check your output in the quiz at the bottom of this page.

  1. What is the length of the string variable verse?
  2. What is the index of the first occurrence of the word 'and' in verse?
  3. What is the index of the last occurrence of the word 'you' in verse?
  4. What is the count of occurrences of the word 'you' in the verse?

You will need to refer to Python's string methods documentation.

Start Quiz:

verse = "If you can keep your head when all about you\n  Are losing theirs and blaming it on you,\nIf you can trust yourself when all men doubt you,\n  But make allowance for their doubting too;\nIf you can wait and not be tired by waiting,\n  Or being lied about, don’t deal in lies,\nOr being hated, don’t give way to hating,\n  And yet don’t look too good, nor talk too wise:"
print(verse)

# Use the appropriate functions and methods to answer the questions above
# Bonus: practice using .format() to output your answers in descriptive messages!

Using String Methods

QUIZ QUESTION::

Please match the correct response to each of the following questions.

ANSWER CHOICES:



Objective

Code

368

370

362

3

10

186

62

65

8

64

SOLUTION:

Objective

Code

362

186

65

8