Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For word = "computer", what are word[0], word[-1], word[2:5] and word[::-1]?
Positions start at 0, negative positions count from the end, and a slice stops before its end position.
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, compare your working with the solution.
Write the positions under the letters: c 0, o 1, m 2, p 3, u 4, t 5, e 6, r 7.
word[0] is c, the first character.word[-1] is r, the last character.word[2:5] has the characters at 2, 3 and 4, which is mpu. It stops before 5.word[::-1] goes backwards through the whole word: retupmoc.Log in to join the discussion.
Loading…