FAQ: Introduction to Strings - Cut Me a Slice of String

This community-built FAQ covers the “Cut Me a Slice of String” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Cut Me a Slice of String

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hey,

The first questions asks to slice the first five letters of last_name, so I try

first_name = "Rodrigo"
last_name = "Villanueva"

new_account = last_name[:4]

but it says that’s wrong, then I look at the hint and it shows new account = last_name[:5].
With what we already learned with index, the first five letters wouldn’t supposed to be from index 0 to index 4 ??
I mean we have 0, 1, 2, 3, 4, that’s first five letters from the string.
Or there’s something I’m missing I don’t know

list slicing has the following syntax:

list[start:stop]

the stop value is not included (its lesser then the stop value, not lesser then or equal too)

which is why we need 5 as stop value, this will give us the elements from indexes 0, 1, 2, 3 and 4

Screenshot 2023-08-02 at 4.33.14 PM
I think this is a horrible way to show how indices work. this would be better if the index number were underneath the letters not before or after. I’m very familiar with how indices work and this completely threw it off for me.

I think the course authors chose to do this to capture the behavior of Python’s slice in as much as that the element at the start index is included in the slice, but the element at the end index is excluded. If the topic under discussion wasn’t slicing but just indexing in general, then the index number should definitely be directly underneath the characters.

Would that not make them labels? The illustration displays them as, boundaries which truth be known is how I glommed onto the concept early on, learning about zero-indexed and unity-indexed linear data structures.

In the above figure, 0 is the left-most boundary, and 9 is the right-most boundary. It might not be accessible (by index) since it is what we OG call, the first non-existent byte, and therefore an EOF.

We can make an index a label but we need to convert to an dict to do that. That is what we use enumeration for. Now the index is a label, but we know it as a key. This would be a convenient case for unity-indexing (starts with 1). The correlation between lists and dictionaries is purely a dichotomy, of sorts.

Bottom line, we have the freedom to use indices, or keys while still maintaining a linear data structure.

>>> def enumerator(n):
...     return [*enumerate(range(n))]
... 
>>> enumerator(10)
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
>>> dict(enumerator(10))
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
>>> {k + 1: v for k, v in enumerator(10)}
{1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8, 10: 9}
>>> 

Fun, fun, fun!

If you were your grandfather someone might have heard that complaint. The author has given a very good depiction of how the indexing system works. Given some thought it adds up.