Slicing Strings - Cut Me A Slice of String Exercise - Trouble with Indices


I am just starting out on the slicing strings Cut Me A Slice exercise and slicing is already confusing to me.
Here is the link to the exercise:

I ran the solution on this exercise and I am confused by how the system generated solution is correct.

The question is: Create a variable called temp_password by creating a slice out of the third through sixth letters of his last_name .

What I understand from this question is to include the characters starting from index 3 and ending at index 7 (to include the sixth letter).

My code would look like this:


The system solution looks like this:

temp_password = last_name[2:6]

Which generates the result ‘llan’.

I’m confused why if the question is asking for a slice that includes the 3rd character would it start with the index 2? If it is asking to include up to the sixth character, wouldn’t the last index need to be 7 so that it includes the 6th character?


1 Like

Everything that is connected with indexes is starting from 0 not one, this is why it’s [2:6] instead of [3:7].
Arrays are the same too, so if I would have a shoppingCart array (or a list) I would access the first item in the array with 0:

shoppingCart = ["banana", "ball", "shoes"]
print(shoppingCart[0]) # would print banana
print(shoppingCart[1]) # would print ball

Hope this helps :grinning:


Yes, that did help. Thank you! I realize now I was reading the question and instead of focusing on the number character it was asking me for, I was thinking of it in terms of the index number.

1 Like

Don’t be suirprised when very shortly we start writing variable indices instead of literal. Thought tip: Think integer sequence when index comes up, and remember to include the negative integers, too.

When n = len(array), -n is in range as an index.

1 Like

I am really confused by this. If you can elaborate on your response, please. Thank you.

1 Like

It was referring to using variables in the syntax, rather than number literals.


where a and b are dynamic.

It was really meant as a booster statement, but now you mention, it doesn’t really come about that way. Belay, belay, or not. When it comes to index versus value we can never get enough practice between the two. Slicing is great practice.