4. Access by Index


#1
  1. Access by Index
    Hello I am new here and really excited to get started with python.
    I am stuck on number 4. access by index.

"""
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!)
"""
c = "cats"[0]
fifth_letter = "O"[1]
n = "ryan"[3]

print fifth_letter


Each character in a string is assigned a number. This number is called the index. Check out the diagram in the editor.

c = "cats"[0]
n = "Ryan"[3]
In the above example, we create a new variable called c and set it to "c", the character at index zero of the string "cats".
Next, we create a new variable called n and set it to "n", the character at index three of the string "Ryan".
In Python, we start counting the index from zero instead of one.

Instructions
On line 13, assign the variable fifth_letter equal to the fifth letter of the string "MONTY".

Remember that the fifth letter is not at index 5. Start counting your indices from zero.

**Thank you for your help**


#2

fifth letter = "MONTY" [4]
the answer will by Y (letter Y). the fifth letter in word MONTY, but remember you are starting indexing from zero 0(M),1(O),2(N),3(T),4(Y).
I hope this will help.


#3

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