Lab Exercise 6 - 10/05/2015

Strings and Their Methods


CS 1510
Introduction to Computing


Introduction

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

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')
    3

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: Generate UNI email addresses

Suppose that every UNI email address was generated from the following formula:

The first 5 characters of your last name followed by the first initial of your first name followed by @uni.edu

For example:

Write a program called generateEmail.py   This program should prompt the user for the first name and then prompt the user for their last name.  It should then print that person's email address using the formula described above.



Task 3: Process a CSV Record (Building block for HW #5 - Files)

Look at splitCSV.html and study the Python code that prints out the 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: Mark Jacobson,20,17,20

    Student Mark Jacobson has a grade of 19.0 points.


    Enter a student record: Mortimur Snurd,20,19,18,17,16,15,14,13

    Student Mortimur Snurd has a grade of 16.5 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 string_split.py 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.

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.