Formatting StringsThe 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 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 String Formatting OperatorTo 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
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 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 5 feet and 2 inches is 1.5748 meters Field Format SpecifiersA field format specifier, which begins with a percent sign
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 print("%6d feet and %6d inches is %1.4f meters" % (feet, inches, meters)) which results in both □□□□□ 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 □□□□□ 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 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 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
Column Formatted OutputFormatted 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 |