Exercise 7.2.6: If You’re Not First, You’re Last Write the function called end_of_word that takes a string and returns everything but the first character.

Exercise 7.2.6: If You’re Not First, You’re Last Write the function called end_of_word that takes a string and returns everything but the first character. end_of_word(“boat”) = “oat” end_of_word(“mice”) = “ice

The Correct Answer and Explanation is:

Here is the correct function definition for Exercise 7.2.6: “If You’re Not First, You’re Last”:

✅ Python Function:

pythonCopyEditdef end_of_word(word):
    return word[1:]

✅ Example Usage:

pythonCopyEditprint(end_of_word("boat"))  # Output: "oat"
print(end_of_word("mice"))  # Output: "ice"

📘 Explanation

The goal of this exercise is to write a Python function called end_of_word that returns a version of a given string without its first character. For instance, given the input "boat", the output should be "oat", and for "mice", the output should be "ice".

1. Understanding String Indexing

In Python, strings are indexed collections of characters. The first character is at index 0, the second at 1, and so on. So for the string "boat", we have:

  • "b" at index 0
  • "o" at index 1
  • "a" at index 2
  • "t" at index 3

To return everything except the first character, we start slicing the string from index 1.

2. Using String Slicing

Python provides a feature called slicing, which lets you extract portions of sequences like strings, lists, and tuples. The syntax is:

pythonCopyEditstring[start:stop]

When you use word[1:], it means:

  • Start at index 1 (skipping the first character).
  • Go all the way to the end of the string.

So word[1:] effectively removes the first character and returns the rest.

3. Function Definition

The function is defined using the def keyword. It takes one parameter word, and returns the result of slicing it from the second character onward.

This exercise helps reinforce your understanding of string indexing and slicing in Python — fundamental concepts in string manipulation and data processing.

Scroll to Top