Professional Documents
Culture Documents
Objectives
● To learn about Python's built-in str type, which represents textual data.
● To gain more experience (1) using the Python shell to build software experiments and (2)
applying Practical Programming's function design recipe (FDR) to develop simple
functions that process text.
Learning outcomes: 1, 4, 5; Graduate attributes: 1.3, 5.3 (see the course outline)
Overview
In this lab's exercises, you'll explore and apply some of the computations Python can perform
with character strings (objects of type str).
In Part A, some exercises will require you to type expressions in the Python shell window, record
the results displayed by Python, and then draw conclusions. For other exercises, you will devise
one or more short experiments. You'll record your experiments (i.e., write the Python
expressions that you typed and the results displayed by Python), then write one or two sentences
that summarize what you learned.
You don't have to submit your solutions to Exercises 1 through 5. We recommend that you bring
a printed copy of this handout to the lab, so that you can record your solutions on it, but you can
type your solutions in a file, if you prefer.
In Part B, you'll use the function design recipe to develop some short text-processing functions.
You'll submit your solutions to Exercises 6 through 8 in Python source code (.py) files.
Remember, you don’t have to finish the exercises during your scheduled Lab 6 session on
Wednesday, Thursday or Friday. You have until Sunday, Sept. 29 at 11:59 pm to complete any
unfinished exercises on your own time and submit your solutions for grading.
Getting Started
Launch Wing 101. When Python starts, it prints a message in the shell window. The first line
should contain “3.7.4”, which indicates that Wing is running Python version 3.7. If another
version is running, ask a TA for help to reconfigure Wing to run Python 3.7.
1
Part A - Software Experiments
Exercise 1: A Python string is a collection of characters. Literal strings are enclosed in pairs of
single quotes or double quotes; for example, 'Hello, world!' and "Hello, world!" are
the same string. This syntax is particularly useful if you want a string literal that contains single
or double quotes; for example, "haven't" or '"Spam, spam, spam," he said.'
>>> type('Hello')
__________________________________________________________
>>> type("goodbye")
__________________________________________________________
Notice that a character string's type is str, regardless of whether it is enclosed in single or
double quotes.
Type the following string literals. Record the values Python displays as it evaluates each
expression:
>>> 'Hello'
__________________________________________________________
>>> "goodbye"
__________________________________________________________
__________________________________________________________
>>> "" (An empty string - type two quotes with no spaces between them.)
__________________________________________________________
Notice that a string literal always evaluates to a string. Notice that the strings displayed by
Python are always enclosed in single-quotes, regardless of whether you typed single quotes or
double quotes, with one exception (a string that contains single quotes).
2
Exercise 2: What operations are permitted with values of type str?
When used with strings, + is the concatenation operator. At the shell prompt, type:
Are *, /, // and - valid operators when both operands are strings? If so, what operations do they
perform? What is the type of each result? Perform some experiments to find out. On the ruled
lines, record the expressions you type, the values that Python displays when it evaluates the
expressions, and your conclusions.
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
3
Exercise 3: The * operator performs replication when one operand is a string and the other
operand is an integer?
>>> "Spam" * 3
_______________________________________________________
>>> 3 * "Spam"
_______________________________________________________
>>> "Spam" * 1
_______________________________________________________
>>> 1 * "Spam"
_______________________________________________________
>>> "Spam" * 0
_______________________________________________________
>>> 0 * "Spam"
_______________________________________________________
>>> "Spam" * -3
_______________________________________________________
>>> -3 * "Spam"
_______________________________________________________
Is the order in which the operands appear important; i.e., does it matter whether the string is the
left-hand or right-hand operand of *?
_______________________________________________________________________
4
Exercise 4: Are +, -, /, and // valid operators when one operand is a string and the other
operand is an integer? If so, what operations do they perform? What is the type of each result?
Does it matter if the string is the left-hand or right-hand operand? Perform some experiments to
find out. On the ruled lines, record the expressions you type, the values that Python displays
when it evaluates the expressions, and your conclusions.
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
5
Exercise 5: Is the string representation of a number equivalent to the number? For example, is
the string '123' the same as the integer 123? Type the following expressions and record the
results:
_______________________________________________________
_______________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Step 1: Create a new editor window and save it as a file named lab6ex6.py.
>>> repeat('yes', 4)
'yesyesyesyes'
>>> repeat('no', 0)
>>> repeat('no', -2)
>>> repeat('yesnomaybe', 3)
"""
Complete the examples in the docstring and then write the body of the function. Your function
definition must have exactly one statement in the function body: return followed by an
expression. Do not use local variables. Use the Python shell to test your function.
6
Exercise 7 (Adapted from Practical Programming, Chapter 4, Exercise 9)
Step 1: Create a new editor window and save it as a file named lab6ex7.py.
Complete the examples in the docstring and then write the body of the function. Your function
definition must have exactly one statement in the function body: return followed by an
expression. Do not use local variables. Use the Python shell to test your function.
Exercise 8
Step 1: Create a new editor window and save it as a file named lab6ex8.py.
Step 2: Use the function design recipe to design, code and test the definition of a function named
replicate. This function has one argument, which is a string. It returns a new string that
contains one or more copies of the argument. The number of copies is equal to the number of
characters in the original string. For example, when called this way: replicate('a'), the
function returns 'a'. When called this way, replicate('abc'), the function returns
'abcabcabc'. (This string contains three copies of 'abc' because the argument has three
characters.)
7
Wrap Up
1. Login to cuLearn and click the link Submit Lab 6 for grading.
3. Submit your solutions to by dragging the files lab6ex6.py, lab6ex7.py and lab6ex8.py
to the File submissions box. Click the Save changes button. The Submissions
status page will be displayed. The line labelled Submission status will indicate Draft (not
submitted), which means that you can make changes. The line labelled File submissions
will list the names of the files you've submitted.
4. If you want to make changes to your submission, click the Edit submission button. You
can then delete or replace any of the files. After you've edited your submission, click the
Save changes button. The Submissions status page will be displayed. (If you log out
of cuLearn after submitting a draft of your solutions, the submission status will still be
Draft (not submitted) the next time you login before the submission due date.)
5. To finish submitting your lab work, click the Submit assignment button.
6. When the Confirm submission page appears, click the Continue button to submit your
work. The status of your submission will change to Submitted for grading. If you've
changed your mind, click the Cancel button. This will return you to the Submission
status page. Completing the submission process is important. You cannot change
the submission status after the due date. Only submissions with status Submitted for
grading will be graded.
Extra Practice
During the midterm and final exams, you will be expected to draw diagrams similar to those
created by Python Tutor. Use PyTutor to visualize the execution of your exercise solutions.
Remember, PyTutor doesn't have a shell. After copying your function definitions into the
PyTutor editor, you will have to type assignment statements that call the functions and assign the
returned values to variables.