CSC111 Lab 6 2011

From dftwiki3
Revision as of 21:18, 12 October 2011 by Thiebaut (talk | contribs) (Challenge 5)
Jump to: navigation, search

This lab deals with Strings, string variables as object instantiated from the class String, which sports many different and useful methods.





More String Methods

PythonYellowLogo.png

The methods are all documented at this page: http://docs.python.org/py3k/library/string.html. Open this page and refer to it while you are working on this lab.

Use emacs or the python interactive prompt to test out different methods.

center()

One of the string methods is the center function. A few people discovered it and used it in the last homework assignment...

Use it to center the title "computer SCIENCE 111" between two lines of 45 dashes. Use a variable called myTitle to store the string.

        myTitle = "computer SCIENCE 111"
        print 45*"-"
        print myTitle.center( 45 )
        print 45*"-"

capitalize()

Instead of printing the variable myTitle, print myTitle.capitalize( ) and see what happens.

upper()

Use upper() instead of capitalize(). Observe the new output.

title()

Same exercise, but with the title() method.

Challenge 1

QuestionMark3.jpg


Write some Python code that will ask for your name, then your last name, and will print both of them centered between two lines of 60 dashes. Make your program capitalize the first letter of each word.
Example
your first name? alexAndra
your last name?  SMITH

------------------------------------------------------------
                      Alexandra Smith                       
------------------------------------------------------------




Replacing substrings in a string

To replace a substring of a string (or a word in a sentence), all you need to do is to use the replace() method.

   sentence = """Strength is the capacity to break a chocolate 
              bar into four pieces with your bare hands - and 
              then eat just one of the pieces"""

   print sentence
   sentence = sentence.replace( "chocolate", "carrot" )
   print sentence

Try it and replace the word "piece" by the word "chunk" in the same sentence, so that now you're breaking carrots into chunks.

Multiple Replacements

We are still dealing with the sentence above, but we want to replace four separate words by four new words. The words we want to replace are in this list:

  toBeReplaced = ["chocolate", "piece", "hand", "break"] 

and the replacement words are in this list:

  newWords = ["carrot", "chunk", "elbow", "melt"]

Challenge 2

QuestionMark2.jpg

Write python statements that will take the string sentence and replace chocolate by carrot, then it will look for piece and replace it by chunk, hand by elbow, and break by melt.

Use a for-loop, something inspired by this code...

oldStr = [ "chocolate", "piece", "hand", "break" ]
for i in range(  len( oldStr ) ):
     print( oldStr[ i ] )



Count()

Read the documentation on the method count() and write some statements that output the number of times the word "piece" appears in sentence using the count() method.

Strip()

Read the documentation about the method strip() and use it to allow you to print the lines below left aligned.

   listOfStrings = [ "          But then, shall I never get any older than I am now?      ",
                           "     That'll be a comfort, one way -- never to be an old woman --          ",
                           "          but then -- always to have lessons to learn!           ",
                           "    Alice    " ]

Splitting strings

First, read the documentation on the split() method.

Let's use the same string as before.

   sentence = """Strength is the capacity to break a chocolate 
              bar into four pieces with your bare hands - and
              then eat just one of the pieces"""

And process it that way

   list1 = sentence.split( "\n" )
   print "list1 = ", list1

   list2 = sentence.split( )
   print "list2 = ", list2

   list3 = sentence.split( 's' )
   print "list3 = ", list3

Challenge 3

QuestionMark4.jpg


Make your program print the number of words in sentence (there are several ways to do that: one long, one very short).







Challenge 4

QuestionMark5.jpg
Make your program display the first and last words of the sentence. (Remember that you can use negative indexes to pick elements of a list starting from its end.)






Challenge 5

QuestionMark1.jpg
Assume that you have a string of this type:
   NYTBestSellers = """1. THE HELP, by Kathryn Stockett
                                2. WORST CASE, by James Patterson and Michael Ledwidge
                                3. THE LOST SYMBOL, by Dan Brown
                                4. POOR LITTLE BITCH GIRL, by Jackie Collins
                                5. WINTER GARDEN, by Kristin Hannah """
Write the code that will take this string, process it, and output the information in a different format, shown below:
  Kathryn Stockett: 1. The Help
  James Patterson and Michael Ledwidge: 2. Worst Case
  Dan Brown: 3. The Lost Symbol
  Jackie Collins: 4. Poor Little Bitch Girl
  Kristin Hannah: 5. Winter Garden