Exercise 04: String Stats
You are going to create a simple program that can display some information about a given string. The string will be a book that you select from the Project Gutenberg.
This exercise is designed to introduce you to:
- lists
- more string functions
Setup
- In your
cs102
folder, create a folder namedex04
for this assignment - Open Thonny and ensure that you already have a code editing tab (usually it says
<untitled>
)- If you do not have an editing tab, press the white paper icon to create one
- Press the blue floppy disk icon to save your file. Make sure name the file
gutenstat.py
and save it in yourex04
folder - Search Project Gutenberg for a book that you like. Choose to view the book as plain-text in your web browser. Highlight all text (Mac: cmd + A, Windows: ctrl + A) and copy it (Mac: cmd + C, Windows: ctrl + C). Back in Thonny, create a variable called
book
and assign it the string you copied. It’s a very long string, so we will use notation to start and end a multiline string ("""
).
Assignment
With your choosen book pasted into Thonny and assiged to the variable book
, you will perform the following tasks.
Print the name of the book to the terminal
Print how often the word
the
occurs. Use the format:the: {INSERT NUMBER OF APPEARANCES HERE}
Print how often the character
s
occurs. Use the formats: {INSERT NUMBER OF APPEARANCES HERE}
Print how many characters are in the book. Use the format
characters: {INSERT NUMBER OF CHARACTERS HERE}
Print how many words are in the book. NOTE: A “word” for our purposes is any text separated by a space
' '
.words: {INSERT NUMBER OF WORDS HERE}
Hints
There are useful string methods that may help you with your tasks.
Submission
Right click your assignment folder and choose compress
on MacOS or Compress to ZIP file
on Windows. Upload the zip file to the matching Moodle assignment to submit your work.
Grading
You will earn up to 10 points for this exercise, broken down as follows:
- 5 points - the program runs correctly and without errors
- 5 points - the program uses count and split