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:

Setup

  1. In your cs102 folder, create a folder named ex04 for this assignment
  2. 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
  3. Press the blue floppy disk icon to save your file. Make sure name the file gutenstat.py and save it in your ex04 folder
  4. 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.

  1. Print the name of the book to the terminal
  2. Print how often the word the occurs. Use the format the: {INSERT NUMBER OF APPEARANCES HERE}
  3. Print how often the character s occurs. Use the format s: {INSERT NUMBER OF APPEARANCES HERE}
  4. Print how many characters are in the book. Use the format characters: {INSERT NUMBER OF CHARACTERS HERE}
  5. Print how many words are in the book. NOTE: A “word” for our purposes is any text separated by a space ' '.

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 5 points for this exercise, broken down as follows: