CS 170 Lab 4: Simple string manipulations in Python

This is a lab exercise to help you get started with doing simple string exercises in Python. In particular, you will learn how to import the string library and use several of the functions that appear in Table 4.2 on page 96 of your Python textbook.

  1. Launch Windows Explorer. View the Y:\CS170 subdirectory that you (I hope) created for the first lab.


  2. Save the file StringStuff.py to your CS 170 directory.


  3. Launch Idle on this file. You do this by right-clicking on the file and choosing Edit with IDLE from the menu. This will open two windows, one with the text of the program and one to run Python.


  4. You might try running this program, just to see what it does. To do this, choose Run Module from the Run menu.


  5. Your main task is to produce a new program, called StringLab.py that asks the user for an input string (using raw_input), then first prints each word in the input in all lowercase, one word per line. Then your program should print the total word count and the average word length (with two digits to the right of the decimal point). Given the input:
    Please say hello to my friend Jane.
    
    your program should output something like:
    Words:
    please
    say
    hello
    to
    my
    friend
    jane. (yes the period will still be here, that is okay)
    
    Word Count: 7
    Average Word Length: 4.14
    
    Functions you are likely to find especially useful include lower, split, and len.


  6. When you have finished modifying your program, you can run it. To do this choose Run Module from the Run menu. If you made any mistakes, you will get some kind of error message. If you need help interpreting an error message, please ask. Otherwise you will be transferred to the Python Shell window and your program will execute. Test your program to convince yourself that it works. Try it on exactly the above example


  7. When you are able to successfully run your program, you are probably done. Go back to the window where you typed in the program. Make sure that you don't want to change anything, then choose Print Window from the File menu. This will print your file to the default printer, which in the lab will be the printer in the room. Be sure that your name is included in the file you print.


  8. You should hand in a printout of your file.