Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

String & Time Modules
String Module

Objective

Compile application data and send out response letters by using the string module.

Now that the material for the new farm construction has been accounted for, it’s time to turn our attention to new personnel for the farm. We have several letters that need to be sent out for job applications, naturally that would require a lot of writing, however we can use the string module to compensate for this allowing us to create templates and format text to make it easier to handle bodies of text.

Importing the string module this time around will come together a bit differently than other modules as we’ll be importing classes that extend the module, and will be executed in the following fashion:

import string from string import Template from string import Formatter

This gives us access to the module itself as well as access to template and formatting classes that include their own functions. For our purposes, 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 function substitute() 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 the Template() class but is more robust and better for larger string sequences. Uses the function format() which allows you to set up a string and inset string data using {} .

Start off by walking to the light X mark and using the read() function while facing the filing cabinet, this will allow you to access folders with the name of various applicants. These folders are present as three (3) list constants named: folder_a , folder_b and folder_c .

Walk to the dark X mark over the green carpet and face the desk. create three (3) lists called: names_a , names_b, and names_c. Use list comprehensions with the string.capwords() function to format proper capitalization for the names. Pair the lists the constants that use the same letters in their name for example for names_a use the string.capswords() with folder_a, like this: names_a = [string.capwords(x) for x in folder_a] . Use this same formula to populate names_b, and names_c .

Create a list named all_names and sum the three (3) name lists, like this: all_names = names_a + names_b + names_c . This will combine all the lists together into one larger list, follow this up by using a Python function named join() this will join all the list items into a string. You can also outline what to put in between each list item by declaring it in quotations, like this: all_names = ", ".join(all_templates) . Use the pre-written write() function with the all_names list to chart down all the names in their correct capitalizations.

Next up, walk up to the gold X mark and face the desk, here we will set up templates. Here we will have two pre-written template objects named red_templates and blue_templates:

red_template = Template( "Hello, $name , thank you for applying for this position.") blue_template = Template( "Esteemed , $name , your application has been approved.")

Each of these templates have a unique message, they however have one in common which is a placeholder labeled $name. The $ symbol is used to outline a placeholder that can be switched out with a string of your choosing, in this case as the title suggests it’s a name. If no value is switched out the the template will merely use the placeholder in the string itself. To complement this, create a list named all_templates .

Create a for loop with four (4) cycles, with this we’ll replace the names in the three (3) names list with the templates with names included. For example, we will be applying the red_template to the names_a list. Use the substitute() function for the Template() class to switch out the $name placeholder in the template with the names in the list.

for x in range(4): names_a[x] = red_template.substitute(name=names_a[x])

Do the same with names_b and names_c, for names_b use the red_template as well and for names_c use the blue_template .

Once all the names lists have been populated with the new templates, it’s time to review them. Create a new list named all_templates and add in all the names lists together, like this: all_templates = names_a + names_b + names_c . Use the same join() function as before to compile all the templated names into readable form, like this: all_templates = "\n".join(all_templates) . Use the pre-written write() function with all_templates to outline all headers for the different names.

Next we walk up to the dark X mark over the red carpet and face the desk, here we will be formatting and writing the first set of letters. Start off my creating two (2) formatter objects named red_1 and red_2 with the class denotation, like this: red_1 = Formatter() , red_2 = Formatter() . For the composition of the letters, create a list named red_letters .

Much like before we’re going to have a for loop with four (4) cycles, we will have two sets of pre-written strings that will be formatted using the format() function and will be adding the names variables to them. These formatted strings will be appended to the red_letters list forming a comprehensive list of letters that will be sent out. For example, the red_1 format objects will be formatted with the names_a list and appended to the red_letters list.

for x in range(4): red_letters.append(red_1.format( "{} Orientation will begin next week, please report to the office..." , names_a[x]))

Do the same with the red_2 format object using it’s pre-written string and add names_b to the loop. Once this has all been set up use the join() function with red_letters to compile the data into a single file, like this: red_letters = "\n".join(red_letters) . Use the write() function with red_letters to chart down all the formatted letters being sent out.

Lastly walk to the dark X mark over the blue carpet and face the desk, here we will be formatting and composing the last set of letters, much like before create two (2) formatter objects named blue_1 and blue_2 with the class denotation, like this: blue_1 = Formatter() , blue_2 = Formatter() . Create a blue_letters list to format and compose all the letters.

Once again create a for loop with four (4) cycles and use the pre-written strings to format the names lists and append them to the blue_letters list using the format() function. In this case, use the pair up blue_ 1 format objects with the names_c list and blue_2 format objects with names_b list. This is all done in the same fashion as over the red carpet, but using different values, showcasing the versatility of using formats and templates. Once finished use the write() function with blue_letters to file the last of the letters and complete the level.

Code book