Problem with index


#1

Hi guys
I have a problem with the lesson about indexes
the tasks is to name the fifth letter of the word “MONTY” but there is no 5 letter because its said that i have to count from 0 instead of 1 while the answer for this task is "Y"
Could someone explain why?
Thanks

https://www.codecademy.com/courses/learn-python/lessons/strings--console-output/exercises/access-by-index?action=lesson_resume&link_content_target=interstitial_undefined


#2

Lists and strings are zero-indexed, so remember that the first element is index[0].

Why?

It goes to offsets. The first element is offset from the left edge by zero elements. The last element is immediately to the left of its right edge index.

A string of length 5 has index[5] at its right edge. It is not included in the string.

0 1 2 3 4 5
M O N T Y

#3

My best solution that I can come up with is that if you do index(5) (or whatever the line of code is, I don’t remember fully) it will just do the last letter

This is my best solution, and probably not the solution


#4

The code comes out as “MONTY”[4] because the fifth letter is not the fifth index. I missed this and had to have the code given to me but the tip on the side says “Remember the fifth letter is not at index 5” meaning that it literally wants you to assign the fifth letter to the string fifth_letter meaning that you have to change the way you look at it. Index 4 - “MONTY”[4] - puts the fifth letter ( Y ) into the string designation of fifth_letter. I’m new at this so if my explanation is clunky, I apologize. This is my first attempt at answering someone else’s question.


#5

There are a number of ways to access the last letter of a string.

"MONTY"[4]

is the obvious one, but these will also work…

"MONTY"[len('monty') - 1]    # see the connection to above?

"MONTY"[-1]                  # reverse index (right to left)

#6

This is either absolute nonsense or I am dumb.?! This is my first day in Python. Maybe I am missing something, but the way it is explained puzzles me.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.