Access by Index


#1


https://www.codecademy.com/courses/python-beginner-sRXwR/0/4?curriculum_id=4f89dab3d788890003000096#

I have absolutely no idea what to do here. There is no sixth number in "MONTY" so i have no idea what the Fifth number is if i am supposed to count starting at zero. It tells me that it is "Y" but i still have no idea what to code.


fifth_letter =


#2

@zinfaren
To index something, it starts by 0.

//let's say we have this string
//      M       O     N     T       Y
//      |       |     |     |       |
//      0       1     2     3       4 (indexing)
//      1       2     3     4       5 (real)

#3

Thank you. But i'm still a bit confused. If it told me to count starting from zero then how is "y" the fifth letter?


#4

@zinfaren
Y (lol) wouldn't y be the fifth letter?

It is the 4th index, but we are talking letters here.


#5

@zinfaren

There's two lines which I've written the numbers. The first line

Is about indexing an element (which starts by 0, you already know)
The second line

Is about the real number. So "M" will be the first letter but it will be index as 0.
Same for "Y" will be fifth letter but will be index as 4.


#6

Y is the fifth letter when counting as normal, so you want the output to be Y. You get this by counting the index numbers of the letters. The first letter is index 0, the second letter is index 1, etc. Basically to find the 5th letter, you look at index number [5-1], or 4.


#7

The answer is

fifth_letter = MONTY[4]


#8

this is what I have:

c = "cats"[0]
The string "PYTHON" has six characters,
numbered 0 to 5, as shown below:

+---+---+---+---+---+---+
| P | Y | T | H | O | N |
+---+---+---+---+---+---+
0 1 2 3 4 5

So if you wanted "Y", you could just type
"PYTHON"[1] (always start counting from 0!)
n = "Ryan"[3]
fifth_letter = "MONTY"[4]


#9

but I am getting the error message: 'invalid syntax line 2'

line 2 is the following:"The string "PYTHON" has six characters,"

Not sure what to do here


#10

never mind i figured it out. and the correct answer is:

c = "cats"[0]
six_letter = "PYTHON"[5]

n = "Ryan"[3]
fifth_letter = "MONTY"[4]


#11

What I wonder is; what is the purpose of this? For instance: It is not really efficient to print the letter "n" by typing:
print "Monty"[2]


#12

@textmaster59538

Well, you can use it to get access to an item of an array in an iteration... There's many way doing it and helps us not repeating ourselves sometime (when the loops come in)


#13

The answer is

fifth_letter = "MONTY"[4]

Maybe it doesn't have quotes. I don't know.


#14

Never mind sorry i didn't realize you already figured it out.:grin:


#15

that is what I have exactly, but it says i didn't use the access operator [], when I did

Code:
"""
The string "PYTHON" has six characters,
numbered 0 to 5, as shown below:

+---+---+---+---+---+---+
| P | Y | T | H | O | N |
+---+---+---+---+---+---+
0 1 2 3 4 5

So if you wanted "Y", you could just type
"PYTHON"[1] (always start counting from 0!)
"""
fifth_letter = "MONTY"[04]

print fifth_letter


#16

The way that they worded the question is terrible. It's just a terrible question.