Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Change cat into bat (a) with a list of letters and (b) with a string. Which one is easier?
Lists can be changed in place. Strings cannot.
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.
letters = ["c", "a", "t"], then letters[0] = "b". The list is now ['b', 'a', 't']. "".join(letters) makes the word bat.word[0] = "b". We build a new string: "b" + word[1:], which is bat.A list is easier to change. A string is fixed, so we make a new one.
Log in to join the discussion.
Loading…