Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
For the text "the cat sat on the mat", write the program lines that show: how many times at occurs, the position of the first sat, the position of the word dog (which is not there), and whether the text starts with the.
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. Use count(), find() and startswith().
at occurs in cat, sat and mat: three times.sat starts at position 8. Count: the is 4 characters (0 to 3), cat is 4 more (4 to 7), so sat starts at 8.dog is not there, so find() gives -1.the, so the answer is True.Check. Run it: 3, 8, -1 and True.
Answer: 3, 8, -1 and True.
Log in to join the discussion.
Loading…