5th letter problem


#1



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


Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: string indices must be integers, not str

I just dont know how to assighn 5th letter value or whatever can someone pleae tellme what to do

"""
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"

print fifth_letter


#2

here:

fifth_letter = "MONTY"

you need to get the fifth letter of MONTY using square brackets, see how the second letter is retrieved here:

"PYTHON"[1]

for inspiration


#3

To expand on the question's example, strings (as well as some other data types) can be indexed. That is to say, each character in the string has a number given to its position, starting at 0, not 1.

Let us look at the example:

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

You can see P is at index position 0 (although it is the first letter), etc. to N, which is at index position 5 (although it is the 6th letter in PYTHON).

Ok, why does this matter? Well, we can access certain indexes/a range of indexes strings based on these index positions. We do this by using square brackets []. If we want to return the character at an index position we put the index position into the brackets.

So the first character is at index position 0, to access it we would put [0] at the end of the string. If we wanted the third letter we would use [2] after the string (remember, the index starts at 0, so will always be less than the actual position of the letter).

Applying those to the example again:

>>> exampleString = "PYTHON"
>>> print exampleString[0]
P
>>> print exampleString[2]
T

## We could even do it when defining the variable

>>> exampleString = "PYTHON"[0]
>>> print exampleString
P
>>> exampleString = "PYTHON"[2]
>>> print exampleString
T

## Or without defining a variable at all
>>> print "PYTHON"[0]
P
>>> print "PYTHON"[2]
T

As I said, you can do more, such as limiting it to all character between index positions, but to avoid confusion I won't go into that.

Hope the above helps you.


#5

You need to make the output 4 ([4]) so that way it is assigned to Y in "MONTY".
Also make sure to check your indices! If it still doesn't work then you need to refresh the page a couple times.
Hope this helps!
:grinning:


#6

thankyou so much :slight_smile:


#7