Indexing Strings

In the code below, the only part I'M having trouble with is word[position]. I know this is something really simple but I've always had trouble understanding these kinds of statements. I know that it print a random letter (position) from (word). What I don't know is why or how it does that. That's the part that never seems to get explained to me. How can you just put [] around part of a statement and everything just work right? Thanks.

Comments

I'm not quite sure how to explain this if you already have it written out as 'word' and 'position'. Given a string "word" and an integer "i", word[i] returns the letter in the ith position. Remember that Python is zero-indexed, though, so for the word "index", you have i -> 0n -> 1d -> 2e -> 3x -> 4Note that there is not position 5, even though len(word) == 5.

Also, putting '-' in an index has a special meaning in Py. That makes your index start at the end of the word instead of at the beginning. So, you get

x -> -1e -> -2d -> -3n -> -4i -> -5

That's nice for when you need to get to the back of a string/list, but you don't know how long it is. I guess you could also use it to count backwards...