4. Access by Index How to do


#1

How do I do the #4 on the Strings & Console Output section? I cant seem to figure it out. Please help!


#2

If you check the example given at the beginning, its shows this...

 c = "cats"[0]

This means that the variable called c is equal to the first letter of the string 'cats'. Looking at that example they then give you a task which is to create a variable called 'fifth letter' and make it equal to the fifth letter of the string 'MONTY'


#3

You mean "Y"? I tried that but it didnt seem to work.


#4

No, you misread the question. What if you wanted to find the 5 letter of the string called

asdasdfaskjfaokfh

You would simply type asdasdfaskjfaokfh[4] and this would print out the letter which is s :grin:

So, the question again say to create a variable called 'fifth letter' and make it equal to the fifth letter of the string 'MONTY'. This is simply done as...

fifth_letter = 'MONTY[4]'


#5

Why 4? What does that mean?


#6

Because they used a 0-based index for counting. Basically, it means that they start counting at number 0 instead of number 1. So...

MONTY
01234

This means that the first letter of MONTY which is M, starts at number 0 . The second letter O starts at number 1. The third letter N starts at number 2. I'm sure you can guess the letter of number 3 :grin:


#7

Ok. Thanks a lot for your help!


#8

numbers = [5, 6, 7, 8]

print "Adding the numbers at indices 0 and 2..."
print numbers[0] + numbers[2]
print "Adding the numbers at indices 1 and 3..."
print numbers[1] + numbers[3]


#9

The correct answer is

fifth_letter = "Monty"[4]


#10

Yeah I think the confusion comes where it gives you the example, but when looking at the actual question it's not using the examples format of letters. It makes sense if you know what you're looking for, but as a beginner it can be a bit confusing when looking at an example that doesn't seem to correlate to the actual answer its looking for.

Looking at it again after having it explained, makes total sense. But it wasn't very clear for me either. Thanks for the assistance!


#11