Lab Exercise 6

Strings and Their Methods

CS 1510
Introduction to Computing


This week is all about strings, in particular string methods. Of course, we also get more practice with loops and choices.

Create a directory on your USB device for this lab, say, lab06, and launch IDLE.

This week, you will submit your shell window at the end of the session. Try not to close it accidentally along the way.

You will also submit a responses.txt file this week. Download this template file and use it to record any answers or predictions asked for in the exercises.

Task 1: Experiment with Methods for Manipulating Case

Throughout the course, we have used built-in Python functions to compute values and to interact with users. Examples include, int(), float(), ord(), chr(), print() and input(). These are all names for operations that we want to execute.

As you read in Chapter 4, Python strings offer another kind of operation, methods. Recall that we invoke methods with a "dot" syntax, like this:

    >>> name = 'eugene'
    >>> name.count('e')

Python provides a number of built-in methods for manipulating the case of strings, among them:

Let's figure out what they do by trying them out. Do not look them up in your textbook. Do not look them up with Google or any other search engine. Instead, try them out in the shell.

Step 1.
Define these strings in the shell.

You may copy the expressions and paste them into your shell, one at a time.

Step 2.
Try all five of the case-manipulating methods on all four of these strings.

Make up some test cases of your own.

Again, do not look them up in your textbook or on-line. Try them in the shell!

Step 3.
Describe what each of the five methods does in your responses file. Be sure that your descriptions point out what is different about the methods.

Task 2: Compute Scrabble Values

Scrabble is a word game in which words are assigned a value based on the values of the characters and their position on the game board. Character values are based roughly on the frequency of the characters in common vocabulary. In the American English version, these point values are:

We now know enough to compute word values in Python. We can process the letters of the word one at a time. For each letter, we decide which point category it belongs in and add that value to a running total. For example:

    Enter a word: eat
    The word eat is worth 3 points.

    Enter a word: checkmate
    The word checkmate is worth 22 points.

    Enter a word: Wallingford
    The word wallingford is worth 19 points.

('Wallingford' isn't a legal word in Scrabble but, hey, it's my class.)

This is a great opportunity to use the in operator with strings:

    >>> four_point_letters = 'hfwvy'
    >>> 'a' in four_point_letters
    >>> char = 'v'
    >>> char in four_point_letters

Step 1.
Download this partially-completed program.

Step 2.
Add the statements necessary to produce the calculate the Scrabble score for the user's word, which is then displayed to the user.

You may assume that user enters only alphabetic characters. Do not assume that they enter only lowercase letters.

Step 3.
Run your completed program with several words of your own choosing. Copy the results from the shell window and paste it into your responses file.

Task 3: Process a CSV Record

In Session 11, we saw code that printed items separated by commas in a string. This sort of processing for "comma-separated values" is useful for working with spreadsheets. It can be even more useful if we can do arithmetic with the items. For example:

    Enter a student record: Eugene Wallingford,20,17,20

    Student Eugene Wallingford has a grade of 19.0 points.

Step 1.
Download this partially-completed program.

Step 2.
Add the statements necessary to produce the output above.

You must use a loop to process the string. You may not use Python lists or any other feature beyond what we have studied in class or Chapters 1-4.

You may use string methods such as find() if you like, or work through the string character by character. You may even use from Session 11 as your starting point.

Step 3.
Run your completed program with the input string in the example above and another example of your own choosing. Copy the result from the shell window and paste it into your responses file.

Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file. Save your shell window as

Submit your files for grading on the electronic submission system, at lab06 -- Strings and Their Methods.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.

Eugene Wallingford ..... ..... October 1, 2014