Teacher's Guide
Chapter 9 / Level 1: String Module
Teaching Objective:
- Students understand how to to properly import string modules and their syntax
- Learns how to implement string module functions and their specific nuances
- Understands how string functions differ from other functions and how they relate to displaying text rather than various calculations
Guide
In this exercise we will be working with the string module and some of its classes/functions. We’ll be using it to process some applications for job hires in the farmland. To activate the module, there are a series imports to do in order to use it’s classes.
import string
from stringimport Template
from stringimport Formatter
For the purposes of this exercise we will be using the following functions and classes:
string.capwords(): This function converts string words to have proper capitalization, making the first letter of a word capitalized and the rest lowercase regardless of how the string is formatted.Template(): This is a string class, it’s used to create a template object. It uses the functionsubstitute()which allows you to set up a string and highlight a location using the$sign to swap out data. This allows you to create modular strings you can use at will.Formatter(): This is a string class, it’s used to to format or compose a string. Works in a similar fashion to theTemplate()class but is more robust and better for larger string sequences. Uses the functionformat()which allows you to set up a string and inset string data using{}.
Start off by walking to the light X mark and use read() to read all the names in the file folders. Next walk to the X mark over the green carpet, create three lists named: names_a , names_b and names_c . Populate these lists with list comprehensions, capitalizing the names in the list constants: folder_a , folder_b and folder_c by using string.capwords(). For example:
names_a = [string.capwords(x)for xin folder_a]
Do the same with names_b / folder_b and names_c / folder_c .
Create a list named all_names to store all the names in the other lists by summing them then use the join() function to merge all the names into a string dividing each item with ", " . Use the all_names string with the pre-written write() function to chart down the names.
all_names = names_a + names_b + names_c
all_names =", ".join(all_names)
await player.write("List of Names:\n%s" % (all_names))
Walk to gold X mark and set up two(2) templates named red_template and blue_template (They will be pre-written in the editor) and create a list named all_templates .
red_template = Template(
"Hello, $name , thank you for applying for this position.")
blue_template = Template(
"Esteemed , $name , your application has been approved.")
all_templates = []
Create a for loop and substitute the names lists with the templates previously created by using the substitute() template class function. For example:
for xinrange(4):
names_a[x] = red_template.substitute(name=names_a[x])
Use the red_template to substitute names_a and names_b , and blue_template with names_c using this format.
Create a list to store all templates named all_templates and sum all the names lists and use the join() to combine all the templated names with each item separated by "\n". Chart down the templates using the write() function.
all_templates = names_a + names_b + names_c
all_templates ="\n".join(all_templates)
await player.write(all_templates)
Walk to the dark X mark over the red carpet and create two (2) format objects named red_1 and red_2 as well as a list to store letters named red_letters .
red_1 = Formatter()
red_2 = Formatter()
red_letters = []
Create a for loop and use the names lists with the format() class function, using the red_1 and red_2 format objects and append it in the red_letters list. Use names_a with the red_1 object and use names_b with the red_2
for xinrange(4):
red_letters.append(red_1.format(
"{} Orientation will begin next week, please report to the office..."
, names_a[x]))
red_letters.append(red_2.format(
"{} We regret to inform you we can not to move forward with your application..."
, names_b[x]))
Use the join() function to convert the red_letters list into a string divided by "\n" and use the write() function to compose the letters.
red_letters ="\n".join(red_letters)
await player.write(red_letters)
Walk to the dark X mark over the red carpet and create two (2) format objects named blue_1 and blue_2 as well as a list to store letters named blue_letters .
blue_1 = Formatter()
blue_2 = Formatter()
blue_letters = []
Create a for loop and use the names lists with the format() class function, using the blue_1 and blue_2 format objects and append it in the blue_letters list. Use names_c with the blue_1 object and use names_b with the blue_2 .
for xinrange(4):
blue_letters.append(blue_1.format(
"{} We will be sending you further documentation for approval..."
, names_c[x]))
blue_letters.append(blue_2.format(
"{} We have another opening for you at a later date if you would like to apply..."
, names_b[x]))
Use the join() function to convert the blue_letters list into a sting divided by "\n" and use the write() function to compose the letters and complete the level.
blue_letters ="\n".join(blue_letters)
await player.write(blue_letters)
Map overview and path

Code Solution
import player
import string
from string import Template
from string import Formatter
player.turn_left()
player.move_forward(2)
player.turn_left()
player.move_forward(2)
await player.read()
player.turn_right()
player.move_forward()
player.turn_left()
names_a = [string.capwords(x) for x in folder_a]
names_b = [string.capwords(x) for x in folder_b]
names_c = [string.capwords(x) for x in folder_c]
all_names = names_a + names_b + names_c
all_names = ", ".join(all_names)
await player.write("List of Names:\n%s" % (all_names))
player.turn_right()
player.move_forward(3)
player.turn_left()
red_template = Template(
"Hello, $name , thank you for applying for this position.")
blue_template = Template(
"Esteemed , $name , your application has been approved.")
all_templates = []
for x in range(4):
names_a[x] = red_template.substitute(name=names_a[x])
names_b[x] = red_template.substitute(name=names_b[x])
names_c[x] = blue_template.substitute(name=names_c[x])
all_templates = names_a + names_b + names_c
all_templates = "\n".join(all_templates)
await player.write(all_templates)
player.turn_left()
player.move_forward()
player.turn_right()
player.move_forward(3)
player.turn_right()
player.move_forward()
player.turn_left()
red_1 = Formatter()
red_2 = Formatter()
red_letters = []
for x in range(4):
red_letters.append(red_1.format(
"{} Orientation will begin next week, please report to the office..."
, names_a[x]))
red_letters.append(red_2.format(
"{} We regret to inform you we can not to move forward with your application..."
, names_b[x]))
red_letters = "\n".join(red_letters)
await player.write(red_letters)
player.turn_left()
player.move_forward(3)
player.turn_right()
blue_1 = Formatter()
blue_2 = Formatter()
blue_letters = []
for x in range(4):
blue_letters.append(blue_1.format(
"{} We will be sending you further documentation for approval..."
, names_c[x]))
blue_letters.append(blue_2.format(
"{} We have another opening for you at a later date if you would like to apply..."
, names_b[x]))
blue_letters = "\n".join(blue_letters)
await player.write(blue_letters)