Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
The variable word = "elephant" has eight letters. Work out each answer on paper, then run the program to check.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Write the positions under the letters: e 0, l 1, e 2, p 3, h 4, a 5, n 6, t 7. The negative positions count from the end: t is -1, n is -2, a is -3, and so on up to e at -8.
word[2] is e, the third letter.word[-3] is a.word[len(word) - 1] is word[7], which is t. It is the same as word[-1].word[-8] is the first letter, e. For a word of 8 letters, -8 is the same as 0.word[1:4] is lep, with positions 1, 2 and 3, so its length is 3.Check. Each position from 0 to 7 has a negative twin: the two numbers differ by the length, 8.
Answer: e, a, t, e and 3.
Log in to join the discussion.
Loading…