CSC207 - Lab 9
Strings of Text
Overview
In this lab we examine some of the methods for manipulating strings of text.
Materials
Step 1
Our first text for today comes from Lewis Carroll's poem "The Walrus and the Carpenter."
s = """The sun was shining on the sea,
Shining with all his might:
He did his very best to make
The billows smooth and bright--
And this was odd, because it was
The middle of the night."""
As text is a major way of communicating with the user and storing information in a manner we can
easily understand, we will examine the built-in methods Python includes for manipulating these
strings.
Step 2
Now that you have some familiarity with string processing in Python, we will analyze a much
larger string from a file.
Download alice-chapter-one.txt
to your
working directory (the default where your files are saved).
Perform the following steps on the string, and record the results in a Google Doc for this lab:
- Open this file through the interpreter and read in the data into a string.
This can be accomplished by typing
file = open("alice-chapter-one.txt", "r")
s = file.read()
- Convert this string to uppercase and record
the last 10 characters.
- Remove all spaces and carriage returns from the string.
- Find and record the percent of remaining characters that are the letter E.
- Find and record the percent of remaining characters that are the letter T.
- How many sentences are there in this text?
- Where is the third mention of the word "SHE"?
- What is the ordinal value of the middle character of the text?
What to Hand In
Write up the answers to the above questions for Part 1 and Part 2 in a Google Doc, and share
this with me.
© Mark Goadrich, Centenary College of Louisiana