In class String Exercises

Answers are here.

Review Slicing of Strings on the Python crib-sheet and the string methods on this page.

A = "Now is the time to panic"

1. using slicing, retrieve "the" from the string A.

2. using pieces of A, create the string: "panic time"

3. Looking at the Python crib-sheet, you can see how to generate the reverse of a string (the characters in reverse order).  Create your own function Reverse(s) which does not use the slicing trick, but rather builds up the string in reverse.

4. Here's a quotation about Teddy Roosevelt's visionary decision to build the Panama Canal: "A MAN, A PLAN, A CANAL, PANAMA!".  Remove the punctuation, and then put this quote into a string variable.  Print the string in reverse, and try to read it.

5. Note the strikeout change below (to make the task much easier). Suppose you want to replace abbreviations with their full-text versions.  For instance, if you see "hp" as a word in a string, replace it with "Harry Potter", whereas if you see "hpc" as a word, replace it with "Hewlett-Packard".  Figure out how to use the replace() string method, which you'll need for this. Assume that a word is surrounded on both sides by a space character, or it's at the beginning of a line or at its end. Create the HP_Replace(s) function that will do the two types of replacements mentioned in the second sentence, above.  Example:

	HP_Replace(' hp nearly destroyed hpc today when his wand touched a side of hpc ') 
		# returns ' Harry Potter nearly destroyed Hewlett-Packard today when his wand touched a side of Hewlett-Packard '