Data Structures Textbook

 

Formatting Strings

The print function is used to display text and data to the shell (or terminal window). It is a common operation that you have used many times. For example, consider the following code segment which extracts a measurement in feet and inches from the user and then computes and prints the equivalent measurement in meters.

feet = int(input("Enter feet: "))
inches = int(input("Enter inches: "))
meters = (feet * 12 + inches) / 39.37
print(feet, "feet and", inches, "inches is", meters, "meters")

If the user enters 5 and 2 for the two input operations, the result will be

   5 feet and 2 inches is 1.5748031496062993 meters

The default behavior of the print function is to display a floating-point value with as many decimal places as necessary to express But what if we don't want it to print all of those decimal places? Perhaps we only want to display the meters measurement to 4 decimal places.

   5 feet and 2 inches is 1.5748 meters

To accomplish this task, we have to produce a formatted string that is then displayed by the print function. Python provides two techniques for formatting strings. In this document, we review the version known as "C-style" formatting, the name of which comes from the fact that it was first introduced by the C programming language. Today, most major programming languages provide this style of string formatting.

String Formatting Operator

To produce the output shown above, with the value of meters displayed to 4 decimal places, we can use the following print operation

print("%d feet and %d inches is %1.4f meters" % (feet, inches, meters) )

The argument passed to the print function is the string formatting operation that will generate a formatted string. It consists of three parts

"\%d feet and \%d inches is \%1.4f meters" % (feet, inches, meters)
|-------------------------------------------------------| |----------------------------|
format string replacement values

The format operator takes a format string to the left and a collection (tuple) of replacement values to the right. The format string is any string consisting of text and special field format specifiers (indicated by a % sign). When the string formatting operation is performed, it will produce a duplicate copy of the format string but with each field specifier replaced by one of the replacement values.

In our example, the field specifiers within the format string

"%d feet and %d inches is %1.4f meters"

will be replaced with the values of feet, inches, and meters, respectively, resulting in a new string that is then printed

   5 feet and 2 inches is 1.5748 meters

Field Format Specifiers

A field format specifier, which begins with a percent sign % and ends with a specific character, indicates a field into which a data value will be displayed. The character used with the field specifier indicates the type of data to be formatted. The three most commonly used field specifiers are

%d integer
%f float
%s string

In addition to the type of data to be formatted, we can also indicate the number of columns used to display the data within the field. For example, if we want to use 6 columns to display the feet and inches values, then we can change the %d specifiers to that shown below

print("%6d feet and %6d inches is %1.4f meters" % (feet, inches, meters))

which results in both feet and inches being displayed within a field consisting of 6 columns (the □ symbol represents a blank space within the field)

□□□□□5 feet and □□□□□2 inches is 1.5748 meters

By default, the data values will be right-justified within the indicated number of columns. To left-justify the data, we use a negative column width. For example,

print("%6d feet and %-6d inches is %1.4f meters" % (feet, inches, meters))

will produce the following with feet right-justfied within 6 columns and inches left-justified within 6 columns

□□□□□5 feet and 2□□□□□ inches is 1.5748 meters

If the field width is not large enough to display the entire data value or if you omit the field width altogether, then the data value will be displayed using as many columns as necessary. Consider the following example

print("The triangle has %2d sides." % (387))

which results in value 387 spilling over the 2 columns specified by the %2d field specifier

   The triangle has 387 sides.

In addition to specifying the total number of columns to use, we can also specify the number of decimal places to use when formatting a floating-point value by appending a decimal point and an integer value to the field width. Thus, in our original example,

print("%d feet and %d inches is %1.4f meters" % (feet, inches, meters))

we indicate that the floating-point value meters is to be displayed using a field width of 1 column and 4 decimal places. Thus, the meters measurement is displayed as

   1.5748

The floating-point field specifier will automatically round the displayed results if the value of the next column (after the one to be displayed is > 5). For example

print("%d feet and %d inches is %1.8f meters" % (feet, inches, meters) )

will result in

   5 feet and 2 inches is 1.57480315 meters

The following table illustrates the results of additional field format specifier examples

Example Result
"%7s" % ("March") □□March
"%-7s" % ("March") March□□
"%7s" % ("December") December
"%-12dX%8d" % (750, 25) 750□□□□□□□□□X□□□□□□25
"%10.3f" % (3.1415926535) □□□□□3.142
"%10.6f" % (3.1415926535) □□3.141593
"%-10.3f" % (3.1415926535) 3.142□□□□□
"%8.0f" % (3.1415926535) □□□□□□□3
"Result = %12.2" % (3.1415926535) Result = □□□□□□□□3.14

Column Formatted Output

Formatted strings are necessary when constructing a table or report consisting of columns. For example, suppose we need to produce the following table from data extracted from a text file. It consists of the total sales per month for some product (the first five months in this example):

  Month        Units Sold  Total Sales
  -------------------------------------
  January             150      12856.54
  February             75       9481.00
  March               225      32011.75
  April               200      28489.00
  May                 168      19234.00

Since the table consists of 3 columns, one for the month name, one for the number of units sold per month and one for the total sales for the given month, we will need 3 field format specifies. Suppose we used the following format string for each row of the table

print("%s %d %1.2f" % (month, units, total))

The results would look as follows:

  January 150 12856.54
  February 75 9481.00
  March 225 32011.75
  April 200 28489.00
  May 168 19234.00

While all of the rows and the data is displayed, it's not aligned properly. To correctly produce this table, we must also specify the width of each column.

print("%13s %9d %12.2f" % (month, units, total))

Now, the results look as follows:

       January        150      12856.54
      February         75       9481.00
         March        225      32011.75
         April        200      28489.00
           May        168      19234.00

Remember, by default, the data values are right-justified within the given number of columns. Here, the numeric values are aligned properly, but not the month names. We need to left-justify the month name within the first column

print("%-13s %9d %12.2f" % (month, units, total))

which now produces the correct results

  January             150      12856.54
  February             75       9481.00
  March               225      32011.75
  April               200      28489.00
  May                 168      19234.00
Print -- Recent Changes
Page last modified on September 16, 2012, at 02:53 PM