Chapter 3, Lesson 3 Teacher Guide

Chapter Three: Input and Output

Teacher Notes - Lesson Three: String Formatting

 

When simple string concatenation does not give you enough control over an output string, the str.format() function can be used for more specific formatting. str.format() can control the width, alignment and format of each string or numeric parameter.

Lesson Objectives

  • Learn to use str.format() with format specifications
  • Understand the basic syntax of the format specifier string
  • Use str.format() to control the width, alignment and truncation of output
  • Use str.format() to display formatted numbers in a variety of notations

Teaching

Activity #1: Lesson Video

Students may optionally view the lesson video, either individually or as a class. The lesson video will introduce and re-enforce the main lesson concepts.

Activity #2: Lesson Text

Students must read the lesson text. The lesson text contains full details about the specific topic and is required reading.

Activity #3: Class Discussion

In a class discussion, make sure your students understand the basic points of the lesson. They should be able to answer the following questions:

  1. In the format string, what set of symbols is used to identify a location where a value is inserted?
  2. How do placeholder numbers allow you to control where values appear in the output?
  3. How do you set the minimum width of an input value?
  4. How to you set the maximum width of an input value?
  5. How do you set the alignment of an input value?
  6. What options do you have for formatting numeric values? How does each work?

Activity #4: Work with Me Exercise

In this exercise, students will practice using the str.format() function to produce neat columns for a table of high scores. The name field will be centered, and the numeric scores should use a thousands separator. Click on the "Solution" button on the code panel to see our solution code.

Assessment

Today's lesson includes an online quiz that should be completed by your students. Please see your Professional Development links for instructions on giving quizzes.

End of Lesson


Last modified: Wednesday, 8 May 2019, 8:10 AM