Lab 7: Strings and dictionaries

The focus of this week's lab is on:



Tasks

Notes

L0. The setup

Create a Lab07 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)

Open VS Code. Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab07 folder.

L1. Creating a string by reading from a file

Create a new Python source code file called lab7_pi.py.

Download pi.txt and place it in your lab folder.

Write code in lab7_pi.py that reads the data from the file and make a single string. Printing it out should yield:
3.141592653589793238462643383279502884197169399375105820974

Do this in two ways:

  1. concatenation (+ operator)
  2. using lists and the join method

Hint: Each line ends with an invisible new line character. You probably want to discard each one. Recall: you can use either indexing or the strip string method to help with this.


L2. Manipulating string delimiters and writing to a file

Download digits.txt and place it in your lab folder.

L2a. Read from file

Write code in a new source code file lab7_digits.py that reads in the data and stores it as one single-line string. When you print out the string, you should get:
1 2 3 4 5 6 7 8 9 10 11 12

L2b. Split and join

Starting from your string from L2a, create a new string using both split and join methods (without using loops) that when printed, gives (again, not in a loop: print(yourString)):

1
2
3
4
5
6
7
8
9
10
11
12

L2c. Write to file

Write your string from L2b to a new text file called digits2.txt. There should be one number per line of the file.


L3. Dictionaries

Let's practice using dictionaries!

L3a: Warm up

In a new file called lab7_dict.py, make a new dictionary and add the following pairs of keys and values:

Print out the values for each of the keys so that your program prints Hi there CS151! on 3 separate lines (or on a single line, if you prefer!).

L3b: Create a dictionary based on text from a file

Comment out your code from the previous part. Download abcs.txt and place it in your lab folder.

Open up the text file in VS Code. For each of the 26 letters in the alphabet, there is a phrase for each of the 26 letters of the alphabet. For example:

a is for apple
b is for banana

Your goal is to create a dictionary with:

Your dictionary should have 26 keys — one for every lowercase letter in the alphabet — and the associated value should be that letter's phrase.

After you create the dictionary, assuming it is called letterDict, add the following test code. Running lab7_dict.py should produce the expected output below:

print(len(letterDict.keys()))  # 26
print(list(letterDict.keys())[:5])  # ['a', 'b', 'c', 'd', 'e']
print(letterDict['a'])  # a is for apple
print(letterDict['b'])  # b is for banana
print(letterDict['z'])  # z is for zebra

Hints:

L3c: Create a dictionary based on multi-line text from a file

Make a new source code file called lab7_dict2.py. Download the file abcs2.txt to your lab directory.

Open up the text file in VS Code. The contents are the same as in the previous subtask, but each phrase spans 3 lines. For example:

a
is for
apple

are the first 3 lines of the file and the text belongs together as a single phrase a is for apple. The next 3 lines make up the text for b is for banana, and so on.

Your goal is to create a dictionary in the same format as the previous subtask. Use the same above tests involving letterDict, they should give you the same outputs.

Tip: We know that the file contains text for 26 letters of the alphabet and that there are 3 lines for each letter's phrase. While we have tended to use a while loop to read from a file, it would be easier to use (nested) for loops here.

L3d: Build ABCs string from phrases

Now that you have your phrase dictionary set up, your next goal is to create a single-line string called abcs with the 26 letters of the alphabet (all next to each other, without spaces) using the values of your dictionary. The letter you are looking for is the 1st character of each value in the dictionary.

Printing your string abcs should yield:

abcdefghijklmnopqrstuvwxyz

Hints:

L3e: Build word list from phrases

Your next task is to create a single string words containing the distinct nouns from each phrase (the last word; e.g. apple, banana, cookie, ...) separated by a space.

Printing your string words should yield:

apple banana cookie donut everyone flipper goat happy igloo jump kook lamb morning noon open potato quiet rooster stamp truck umbrella viking water xylitol yippie zebra 

Hints:

Running lab7_dict2.py should print out both abcs and words.

Get started with the project

Once you are done with each of the lab tasks, you may start on the project.


© 2021 Oliver Layton