Payal's Chapter 6 Strings Exercises

by Payal Patel

07 Feb 2016

Chapter 6, Exercise 1:

Chapter 6, Exercise 3:

Chapter 6, Exercise 4:

Chapter 6, Exercise 5:

Reflection:

While working on these exercises I found I was able to create these programs without a major struggle. If I ran into a problem I was able to work out of it, however, while working on exercise four I started getting confused on how indices worked in python. I thought the first index from the left started with 0 and then went up to however many characters the string has, but when I tried to do this in exercise 4 I didn’t get the results I was expecting.

In exercise 4, I had the string ‘banana’ stored in the variable fruit. I first tried print fruit.count(‘a’, 0, 5), expecting to get 3 as a result, but this was not the case. Instead my output was 2 for this command. I then tried print fruit.count(‘a’, 1,6). This resulted in an output of 3 (what I was initially expecting and the correct answer for the number of “a’s” in ‘banana’). So, my question here is, are indices started from 1 in Python? (Or does it just work that way for the count method in Python?)

Payal is a junior at UNC-Chapel Hill majoring in Information Science. Find Payal Patel on Twitter, Github, and on the web.