Javairia's Chapter 6 Exercises

by Javairia Warrich

08 Feb 2016

Exercise 1: Write a while loop that starts at the last character in the string and works its way backwards to the first character in the string, printing each letter on a separate line, except backwards.

Exercise 3: Encapsulate this code in a function named counter, and generalize it so that it accepts the string and the letter as arguments.

Exercise 4: There is a string method called count that is similar to the function in the previous exercise. Read the documentation of this method at https://docs.python.org/3.5/library/stdtypes.html#string-methods and write an invocation that counts the number of times the letter a occurs in “banana”.

Exercise 5: Take the following Python code that stores a string:`str = ‘X-DSPAM-Confidence: 0.8475’ Use find and string slicing to extract the portion of the string after the colon character and then use the float function to convert the extracted string into a floating point number.

Reflection: These exercises were fun to do. They made me apply my basic knowledge of strings to programs that simple, but effective. My favorite thing was applying ideas such as splicing and find. Overall, all the concepts will come in handy when writing future programs.

Javairia is a Junior majoring in Information Science at UNC Chapel Hill. Her hobbies include watching sports (Go Panthers!) and reading. Find Javairia Warrich on Twitter, Github, and on the web.