Indexes


#1

It wants me to assign the variable fith_letter to equal to the 5th letter of MONTY


#2

@andrewvin02: The fifth letter of your string is currently at position [4] because in programming we go from zero to string's length - 1 while in the real world we go from 1 to string's length.


#3

@g4be so whats the letter in the string "MONTY"


#4

here is the piece of code in the consule


#5

@antonnifo: In this exercise you're not supposed to assign the value "Y" to fifth letter like that, instead you were supposed to do the following:

fifth_letter = "MONTY"[4]

You have a string so to access a given position, suppose we don't know what's inside of it, do like the example above.


#6

It worked, but I don't understand why.


#7

@judiak: What you don't understand ?


#8

I get why we type "Monty," but why [4]?


#9

@judiak: Different, abit, from the real world, with strings and lists we start from position zero and go until string's length - 1 so to access the fifth letter on our string, which has actually five letters, we use [4] because four is the last index in our string.

  Coding
|M|O|N|T|Y|
 0 1 2 3 4
Real life
|M|O|N|T|Y|
 1 2 3 4 5

#10

Oh! Alright. I get it.