Learning objectives
By the end of this section you should be able to
- Format a string template using input arguments.
- Use
format()
to generate numerical formats based on a given template.
String format specification
Python provides string substitutions syntax for formatting strings with input arguments. Formatting string includes specifying string pattern rules and modifying the string according to the formatting specification. Examples of formatting strings include using patterns for building different string values and specifying modification rules for the string's length and alignment.
String formatting with replacement fields
Replacement fields are used to define a pattern for creating multiple string values that comply with a given format. The example below shows two string values that use the same template for making requests to different individuals for taking different courses.
Example 8.5
String values from the same template
Dear John, I'd like to take a programming course with Prof. Potter. Dear Kishwar, I'd like to take a math course with Prof. Robinson.
In the example above, replacement fields are 1) the name of the individual the request is being made to, 2) title of the course, and 3) the name of the instructor. To create a template, replacement fields can be added with {}
to show a placeholder for user input. The format() method is used to pass inputs for replacement fields in a string template.
Example 8.6
String template formatting for course enrollment requests
A string template with replacement fields is defined below to create string values with different input arguments. The format()
method is used to pass inputs to the template in the same order.
s = "Dear {}, I'd like to take a {} course with Prof. {}."
print(s)
print(s.format("John", "programming", "Potter"))
print(s.format("Kishwar", "math", "Robinson"))
The above code's output is:
Dear {}, I'd like to take a {} course with Prof. {}. Dear John, I'd like to take a programming course with Prof. Potter. Dear Kishwar, I'd like to take a math course with Prof. Robinson.
Concepts in Practice
String template and formatting
Named replacement fields
Replacement fields can be tagged with a label, called named replacement fields, for ease of access and code readability. The example below illustrates how named replacement fields can be used in string templates.
Example 8.7
Season weather template using named replacement fields
A named replacement argument is a convenient way of assigning name tags to replacement fields and passing values associated with replacement fields using corresponding names (instead of passing values in order).
s = "Weather in {season} is {temperature}."
print(s)
print(s.format(season = "summer", temperature = "hot"))
print(s.format(season = "winter", temperature = "cold"))
The above code's output is:
Weather in {season} is {temperature}. Weather in summer is hot. Weather in winter is cold.
Multiple use of a named argument
Since named replacement fields are referred to using a name key, a named replacement field can appear and be used more than once in the template. Also, positional ordering is not necessary when named replacement fields are used.
s = "Weather in {season} is {temperature}; very very {temperature}."
print(s)
print(s.format(season = "summer", temperature = "hot"))
print(s.format(temperature = "cold", season = "winter"))
The above code's output is:
Weather in {season} is {temperature}; very very {temperature}. Weather in summer is hot; very very hot. Weather in winter is cold; very very cold.
Concepts in Practice
Named replacement field examples
Numbered replacement fields
Python's string format()
method can use positional ordering to match the numbered arguments. The replacement fields that use the positional ordering of arguments are called numbered replacement fields. The indexing of the arguments starts from 0. Ex: print("{1}{0}".format("Home", "Welcome"))
outputs the string value "Welcome Home"
as the first argument. "Home"
is at index 0, and the second argument, "Welcome"
, is at index 1. Replacing these arguments in the order of "{1}{0}"
creates the string "Welcome Home"
.
Numbered replacement fields can use argument's values for multiple replacement fields by using the same argument index. The example below illustrates how an argument is used for more than one numbered replacement field.
Example 8.8
Numbered replacement field to build a phrase
Numbered replacement fields are used in this example to build phrases like "very very cold"
or "very hot"
.
template1 = "{0} {0} {1}"
template2 = "{0} {1}"
print(template1.format("very", "cold"))
print(template2.format("very", "hot"))
The above code's output is:
very very cold very hot
String length and alignment formatting
Formatting the string length may be needed for standardizing the output style when multiple string values of the same context are being created and printed. The example below shows a use case of string formatting in printing a table with minimum-length columns and specific alignment.
Example 8.9
A formatted table of a class roster
A formatted table of a class roster
Student Name Major Grade ---------------------------------------------- Manoj Sara Computer Science A- Gabriel Wang Electrical Engineering A Alex Narayanan Social Sciences A+
In the example above, the table is formatted into three columns. The first column takes up 15 characters and is left-aligned. The second column uses 25 characters and is center-aligned, and the last column uses two characters and is right aligned. Alignment and length format specifications controls are used to create the formatted table.
The field width in string format specification is used to specify the minimum length of the given string. If the string is shorter than the given minimum length, the string will be padded by space characters. A field width is included in the format specification field using an integer after a colon. Ex: {name:15}
specifies that the minimum length of the string values that are passed to the name
field is 15.
Since the field width can be used to specify the minimum length of a string, the string can be padded with space characters from right, left, or both to be left-aligned, right-aligned, and centered, respectively. The string alignment type is specified using <
, >
, or ^
characters after the colon when field length is specified. Ex: {name:^20}
specifies a named replacement field with the minimum length of 20 characters that is center-aligned.
Alignment Type | Symbol | Example | Output |
---|---|---|---|
Left-aligned |
|
|
|
Right-aligned |
|
|
|
Centered | ^ |
|
|
Concepts in Practice
Specifying field width and alignment
Formatting numbers
The format()
method can be used to format numerical values. Numerical values can be padded to have a given minimum length, precision, and sign character. The syntax for modifying numeric values follows the {[index]:[width][.precision][type]}
structure. In the given syntax,
- The
index
field refers to the index of the argument. - The
width
field refers to the minimum length of the string. - The
precision
field refers to the floating-point precision of the given number. - The
type
field shows the type of the input that is passed to theformat()
method. Floating-point and decimal inputs are identified by"f"
and"d"
, respectively. String values are also identified by"s"
.
The table below summarizes formatting options for modifying numeric values.
Example | Output | Explanation |
---|---|---|
|
|
The format specification |
|
|
The format specification |
|
|
The format specification |
|
|
The format specification |
|
|
The format specification |
Concepts in Practice
Numeric value formatting examples
Try It
Formatting a list of numbers
Given a list of numbers (floating-point or integer), print numbers with two decimal place precision and at least six characters.
Input: [12.5, 2]
Prints: 012.50 002:00