Week05 Lab05 StringProcessing
Week05 Lab05 StringProcessing
Week05 Lab05 StringProcessing
Introduction
So far we have considered strings as a whole word. However, they can be treated as separate
characters, indexed, and processed. We will be examining the various ways strings can be manipulated.
One may also do some task for each element in a list; for loops are especially useful for this task. In
this lab we will work examples to gain familiarity with strings and lists, build on use of functions, explore
mutability, and use for loops to process each item of a loop.
Learning Objectives.
Upon completing this lab, students should be able to:
1. river = ‘Mississippi’
a. river [2] _____
b. river [4] _____
c. river [9] _____
2. river = ’Connecticut’
a. river [2] _____
b. river [4] _____
1
c. river [1] _____
d. river [3] _____
3. river = ‘Salmon’
a. river [-6] _____
b. river [-2] _____
c. river [-1] _____
For the following problems, write the indices of the string as above, and apply the slice operator. Try to
figure it out, and then test it with Python to check your answer.
1. river = ‘Missouri’
a. river [3 : 7] ________
3. river = ‘Yellowstone’
a. river [ :6] _____
b. river [7: ] _____
c. river [6: ] _____
2
Part IV. Converting to and From Unicode
Python has two built in functions ord() and chr() which will translate words into and out of
the Unicode (numeric) representation of the character. Since they are recorded in order, this can be
useful for indexing over the letters. Note that ord() takes strings, whereas chr() takes integers.
1. Get the Unicode equivalent for the following letters using the ord()function.
a. A g. a
b. B h. b
c. C i. c
d. X j. x
e. Y k. y
f. Z l. z
2. Show that the Unicode representation can be decoded back to its letter representation using
chr().
3. What is the range of the capital and lower case letters?
3
Additional Exercises.
Exercise 1. Word within a Word. Many longer words often contain letters that can be used to make
shorter words. For example, in part B, words within a compound word were extracted. Pick a long
word, and using the results of at least three slices to make a new word. Then, give your word and slices
to your partner and try to figure out each other’s word.
Exercise 2. Reverse Minus. The function reverse (Part V above) finds the reverse of a string using an
accumulator as the loop moves over a string starting with the first character. Put the code in a program
and modify it so that it instead reverses by processing the last character first to reverse the string.