String Slicing using Negative Indices: Behind the Scenes

📌 String Slicing using Negative Indices: Behind the Scenes ⭐️


Welcome to this tutorial 👋 Now you will learn how to slice strings using negative Indices. Let's begin!
You already know that strings have an internal structure that is very similar to a grid (please read this tutorial first if you are not familiar with this. You can also visualize it below). But did you know that you can also refer to indices using negative numbers? Yes, this surprised me very much as well! 😱

This is the grid structure of the "Hello, World!" string with its corresponding positive indices:

diagram 1

You can now see the grid structure in the diagram below, with the corresponding negative indices:

diagram 2

Notice that now we are counting backwards, starting from -1 for the last index all the way to -13 for the first index. We subtract one when we move one index to the left (or add one when we move one index to the right, which is equivalent).

diagram 3

String slicing using negative indices works exactly the same as string slicing with positive indices.
We can "customize" three parameters:
  • start
  • end
  • step
You can omit them as well and the default values will be used.

diagram 4

✨ Examples:


Let’s see some examples:
We will work with the string "Hello, World!". In the diagrams, I use str to refer to the actual string or to a variable that references the string.
First (in the diagram below), we have a slice that starts at index -11, ends at index -5, and has a step of 1. So we will start from index -11, which corresponds to the character “l” (the green dots below the indices indicate the characters that are included in the slice). Then, we move towards the right because the step is positive (+1), including the subsequent characters until we reach index -6, because the character at the end index (index -5) is not included.
💡 Note: This rule still applies when we are working with negative indices, the character at the end index is not included.

diagram 5

Here (below) we have the same example, but now we omit the step to show that 1 is still the default value for the step, even if we are working with negative indices. Notice that we start at index -11, move to the right, and end at index -5 (not included).

diagram 6

✨ Positive vs. Negative Step


The slice will be created from left to right or from right to left, depending on the sign of the step. If the step is positive, it will move from left to right. If the step is negative, it will move from right to left.
Therefore, for the string to be non-empty when we work with negative indices, the start index must be located to the right of the end index because we would move from right to left. You will see an example of this below.

diagram 7

In this example below, we use a negative index -1. We start from index -5 (included) with character “o” and end at index -11 (not included). We move from right to left because the step is negative, we subtract one from the current index. Please note the green dots below the indices. These are characters included in the slice. You can see the output when this line runs in IDLE below.

diagram 8

Now… the example that I promised you! What happens when the step is negative (we move from right to left) and the start index is located to the left of the end index? The string will be empty. We start at index -11, at character “l”. Sincestep is negative, we start moving from right to left by subtracting one to the current character. But… we reach the first element in the string and we need to reach index -5, but we cannot. This is why an empty string is returned.

diagram 9

✨ Using the Default Values


In the next examples, you will see how we can use the default values of start and end to work with negative indices:
Here we have a slice that starts at index -5, at character “o” (please see the green dots below). Since we don’t specify the end or step parameters, the default values are used. The default value for end is the last index in the string (inclusive) and the default value for step is 1. You can see how we move to the right with a step of 1 until we reach the last index in the string.

diagram 10

Here we have the opposite, we don’t set the start and step indices, but we do set an end index. By default, start is the first element in the string (inclusive) and step is 1. You can see how it moves from index -13, the first one in the string, up to index -9, where the character “o’ is located (not included in the final slice). Please notice the green dots below to visualize this process.

diagram 11

For our final example, we have a slice that uses the default values of start and end and sets a step of -2. Since the step is negative, we move from right to left, “skipping” every other character (we add -2 to the current index). In this case, we start from the last index and end at the first index (inclusive).

diagram 12

I really hope that you liked this tutorial and found it helpful 🙂
Please do not hesitate to ask if you have any questions. You can post your questions on the discussion forums or right below this post. You classmates and Community TAs will always be here to help you. We wish you much success. ✨

Estefania

Comments

  1. Thank you ma’am for this wonderful slides for explanation.
    Where can I finding every week’s post ? I would be glad to know that

    ReplyDelete
  2. Thank you mam for wonderful explanation, I was confused about slicing using negative indices specifically.

    ReplyDelete

Post a Comment