Pure Programmer
Blue Matrix


Cluster Map

Output Formatting

We have seen the basic ability to convert values to strings and output them to the console ([[stdout]]). Sometimes, however, we would like more control on the formatting used to output values. Fortunately we have formatted output functions that help us with that goal.

C-Style printf()

In the olden days when the C language was developed in the 1970's it contained a novel new way of formatting output called the printf() function. This function allows one to specify a format string followed by values that will be substituted into the format string based on the type specifiers found in the format string. This approach has proven very popular and has been copied in many languages since.

With formatted output we must provide a format string that can contain literal text interspersed with type specifiers. The type specifiers are placeholders that identify where and how a value is to be formatted and inserted into the string on output. Type specifiers start with a percent sign (%), followed by an optional flag, followed by field width and/or precision, followed by one or two characters identifying the type of the value (called the type specifier). Only the percent sign and the type specifier is required, the other components are optional.

%[flag][width][.precision][type specifier]

In addition to the format string, we also provide the values to be formatted to the function. We can have multiple type specifiers in the format string as long as we provide the matching number of additional arguments. The first argument is formatted by the first type specifier, the second argument by the second type specifier, and so on. Python implments a form of printf() using the % operator, but this is a now deprecated form. The "[[Pure Programmer Python Utils Module]]" library from this site provides a wrapper so that we can write printf() in our programs.

printf() Illustration
printf() specifiers must match arguments in order

Any other characters in the format string that are not part of a type specifier are printed verbatim. For example:

print "Page count is %d\n" % numPages
print "PI: %.2f" % pi
print "Title and Page Count: %s %d" % (title, numPages)
print "%d of %d" % (currentPage, numPages)

or alternately using the [[Pure Programmer Python Utils Module]] in the same directory as your program.

from Utils import *

printf("PI: %.2f\n", pi)
printf("Title and Page Count: %s %d\n", title, numPages)
printf("%d of %d\n", currentPage, numPages)

Some of the type specifiers available are listed in the table below.

Frequently Used Type Specifiers
SpecifierValue TypeDescription
dsigned integerSigned decimal
xunsigned integerHexadecimal (lowercase)
Xunsigned integerHexadecimal (uppercase)
ffloating pointDecimal floating point
efloating pointScientific notation (lowercase)
Efloating pointScientific notation (uppercase)
gfloating pointShortest representation: %e or %f
Gfloating pointShortest representation: %E or %F
cinteger or single character stringSingle character
sanyString of characters
%noneOutputs literal %
Print Flags
FlagDescription
-Left-justify within the given field width; Right justification is the default.
+Result is preceeded with a plus or minus sign (+ or -) even for positive numbers. By default, only negative numbers are preceded with a - sign.
#Used with o, x or X specifiers the value is preceeded with 0, 0x or 0X respectively. Used with e, E, f, g or G it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
0Left-pads the number with zeroes (0) instead of spaces

The field width and precision are simply integer values separated by a period (.). One may use either in the type specifier, both or none. Field width sets the number of characters that will be used to output the value padded with spaces if necessary to achieve the field width. The precision specifier is only useful for formatting floating point and string values. For floating point values it limits the number of decimal places after the decimal point. For strings it truncates the string to the specified number of characters.

OutputFormatting1.py
#!/usr/bin/env python3;
import Utils

# Begin Main
a = 326
b = -1
c = 2015
i1 = 65000
i2 = -2
i3 = 3261963
f1 = 3.1415926
f2 = 2.99792458e9
f3 = 1.234e-4
c1 = ord("A"[0])
c2 = ord("B"[0])
c3 = ord("C"[0])
s1 = "Apples"
s2 = "and"
s3 = "Bananas"
b1 = True
b2 = False

Utils.printf("Decimals: %d %d %d\n", a, b, c)
Utils.printf("Hexadecimals: %#x %#x %#x\n", a, b, c)
Utils.printf("Long Decimals: %d %d %d\n", i1, i2, i3)
Utils.printf("Long Hexadecimals: %016x %016x %016x\n", i1, i2, i3)

Utils.printf("Fixed FP: %f %f %f\n", f1, f2, f3)
Utils.printf("Exponential FP: %e %e %e\n", f1, f2, f3)
Utils.printf("General FP: %g %g %g\n", f1, f2, f3)
Utils.printf("General FP with precision: %.2g %.2g %.2g\n", f1, f2, f3)

Utils.printf("Boolean: %s %s\n", b1, b2)
Utils.printf("Character: %c %c %c\n", c1, c2, c3)
Utils.printf("String: %s %s %s\n", s1, s2, s3)

Output
$ python3 OutputFormatting1.py Decimals: 326 -1 2015 Hexadecimals: 0x146 -0x1 0x7df Long Decimals: 65000 -2 3261963 Long Hexadecimals: 000000000000fde8 -000000000000002 000000000031c60b Fixed FP: 3.141593 2997924580.000000 0.000123 Exponential FP: 3.141593e+00 2.997925e+09 1.234000e-04 General FP: 3.14159 2.99792e+09 0.0001234 General FP with precision: 3.1 3e+09 0.00012 Boolean: True False Character: A B C String: Apples and Bananas

C-Style sprintf()

Along with the printf() function, the C language introduced a similar function called sprintf(). Instead of printing a formatted string to the console ([[stdout]]), it formats and then returns a string. This more general form has many uses not only for console output but for [[GUI]] output and file output.

OutputFormatting2.py
#!/usr/bin/env python3;
import Utils

# Begin Main
a = 326
b = -1
c = 2015
i1 = 65000
i2 = -2
i3 = 3261963
f1 = 3.1415926
f2 = 2.99792458e9
f3 = 1.234e-4
c1 = ord("A"[0])
c2 = ord("B"[0])
c3 = ord("C"[0])
s1 = "Apples"
s2 = "and"
s3 = "Bananas"
b1 = True
b2 = False

s = Utils.sprintf("Decimals: %d %d %d", a, b, c)
print(s)
s = Utils.sprintf("Hexadecimals: %#x %#x %#x", a, b, c)
print(s)
s = Utils.sprintf("Long Decimals: %d %d %d", i1, i2, i3)
print(s)
s = Utils.sprintf("Long Hexadecimals: %016x %016x %016x", i1, i2, i3)
print(s)

s = Utils.sprintf("Fixed FP: %f %f %f", f1, f2, f3)
print(s)
s = Utils.sprintf("Exponential FP: %e %e %e", f1, f2, f3)
print(s)
s = Utils.sprintf("General FP: %g %g %g", f1, f2, f3)
print(s)
s = Utils.sprintf("General FP with precision: %.2g %.2g %.2g", f1, f2, f3)
print(s)

s = Utils.sprintf("Boolean: %s %s", b1, b2)
print(s)
s = Utils.sprintf("Character: %c %c %c", c1, c2, c3)
print(s)
s = Utils.sprintf("String: %s %s %s", s1, s2, s3)
print(s)

Output
$ python3 OutputFormatting2.py Decimals: 326 -1 2015 Hexadecimals: 0x146 -0x1 0x7df Long Decimals: 65000 -2 3261963 Long Hexadecimals: 000000000000fde8 -000000000000002 000000000031c60b Fixed FP: 3.141593 2997924580.000000 0.000123 Exponential FP: 3.141593e+00 2.997925e+09 1.234000e-04 General FP: 3.14159 2.99792e+09 0.0001234 General FP with precision: 3.1 3e+09 0.00012 Boolean: True False Character: A B C String: Apples and Bananas

String Interpolation

Many languages have the ability to replace variables written directly into strings. This variable substitution in strings is know as variable interpolation. In Python we can substitute varibles inside strings that have the prefix f. One simply wraps the varible name in curly brackets and places it in the string such as {variable_name}. This interpolation mechanism also supports simple expressions in addition to a single variable name.

OutputFormatting3.py
#!/usr/bin/env python3;
# Begin Main
FICA_RATE = 0.0765
PAY_RATE = 20.

annual_salary = PAY_RATE * 2080.0
print(f"FICA Tax Rate: {FICA_RATE}")
print(f"Annual Salary: {annual_salary}")
print(f"FICA Tax: {FICA_RATE * annual_salary}")

Output
$ python3 OutputFormatting3.py FICA Tax Rate: 0.0765 Annual Salary: 50000 FICA Tax: 3825.0 $ python3 OutputFormatting3.py FICA Tax Rate: 0.0765 Annual Salary: 41600 FICA Tax: 3182.4 $ python3 OutputFormatting3.py FICA Tax Rate: 0.0765 Annual Salary: 41600.0 FICA Tax: 3182.4 $ python3 OutputFormatting3.py FICA Tax Rate: 0.0765 Annual Salary: 41600.0 FICA Tax: 3182.4 $ python3 OutputFormatting3.py FICA Tax Rate: 0.0765 Annual Salary: 41600.0 FICA Tax: 3182.4

Modern Message Formatting

The methods for formatting strings and output discuss so far have some limitations when it comes to localizing software. The positional approaches taken by the printf() style functions poses difficulties to localization because often during translation the order of words and thus the substition specifiers must change but the hard-code argument list in our code can not change to match. Variable interpolation has its drawbacks because you are actually putting code into the strings. When externalizing the strings (removing them from the code and putting them in a separate file) necessary for localization, it is not desirable to export variables and expressions from our code to the localization file where they can be changed.

This is why message formatting approaches have been developed that use substitution specifiers that specify which argument to the message format function is to be used. This allows the substitution specifiers to be in a different order (and perhaps re-ordered during localization) than the formal arguments to the message format function. These substitution specifiers are also not executable code as is the case with variable interpolation so it is much safer to externalize from our program code as we will see in the section on Internationalization.

Python has format() method on strings that is the model for use in other languages. The example below shows how you can access the format() method with any string.

#!/usr/bin/env python

first = "First"
middle = "Middle"
last = "Last"

s = "{2}, {0} {1}".format(first, middle, last);
print(s);
Python format() Example

The format() method uses a pair of curly brackets to identify substition placeholders in the format string. Each pair of curly brackets contains a number from 0 to the number of addtional arguments - 1. This number refers to the position in the argument list of the value that will be used in the substitution. This allows values in the argument list to be used in any order needed in the format string or even used more than once.

format() Illustration
format() specifiers can match any argument

Following the position number in the substitution placeholder is an optional type specifier. The type specifier is separated from the position number by a colon. If you don't wish to use a type specifier then the type is assumed to be string. Some of the type specifiers available are listed in the table below.

Frequently Used Type Specifiers
SpecifierValue TypeDescription
bintegerbinary
dintegerdecimal
ointegeroctal
xintegerhexadecimal (lowercase)
Xintegerhexadecimal (uppercase)
fdoublefixed notation
edoublescientific notation (lowercase)
Edoublescientific notation (uppercase)
gdoubleshortest representation: e or f
Gdoubleshortest representation: E or f
cintegersingle character
sstringstring of characters

Like we have seen in printf(), the format() specifiers can take optional modifiers that change how the value is to be formatted. The full definition of the specifiers, including optional components is as follows:

{[arg-pos]:[fill-and-align][sign][#][0][width][.precision][type specifier]}

The fill-and-align option is an optional fill character (which can be any character other than { or }), followed by one of the align options <, >, ^. If no fill character is specified, then the space character is used.

Align Options
OptionDescription
<Left-justify within the given field width. This is the default for non-numeric types.
>Right-justify within the given field width. This is the default for numeric types.
^Aligns the value in the center of the field.

The sign option is one of +, - or the space character.

Sign Options
OptionDescription
+Signifies that a sign character should be output for all values (+ for positive and - for negative).
-Signifies that a sign character should be output for negative values only. This is the default for numeric types.
<space>Signifies that a leading space should be used for positive values and a - should be output for negative values.

The # option causes alternate formatting to be used.

The 0 option pads the field with leading zeros (following any indication of sign or base) to the field width. If the 0 option and an align option both are used, the 0 option is ignored.

The width option is a positive decimal number. If present, it specifies the minimum field width. If the formatted value can not fit within the field width the entire value will be inserted causing the field to be larger than width.

The precision option is a . followed by a non-negative decimal number. This option indicates the precision to use when formatting a value. For floating-point types, precision specifies the formatting precision, i.e. the number of places after the decimal point to display. For string types, it specifies how many characters from the string will be used.

OutputFormatting4.py
#!/usr/bin/env python3;
import Utils

# Begin Main
first = "First"
middle = "Middle"
lastname = "Last"
left = "Left"
center = "Center"
right = "Right"
favorite = "kerfuffle"
i1 = 3261963
i2 = -42
fp1 = 3.1415926
fp2 = 2.99792458e9
fp3 = -1.234e-4

s = "{2}, {0} {1}".format(first, middle, lastname)
print(s)
s = "{0} {1} {2}".format(left, center, right)
print(s)
s = "Favorite number is {0}".format(i1)
print(s)
s = "Favorite FP is {0}".format(fp1)
print(s)

c = ord(favorite[0])
print("Favorite c is {0:c}".format(c))
print("Favorite 11c is   |{0:11c}|".format(c))
print("Favorite <11c is  |{0:<11c}|".format(c))
print("Favorite ^11c is  |{0:^11c}|".format(c))
print("Favorite >11c is  |{0:>11c}|".format(c))
print("Favorite .<11c is |{0:.<11c}|".format(c))
print("Favorite _^11c is |{0:_^11c}|".format(c))
print("Favorite  >11c is |{0: >11c}|".format(c))

c = 0x1F92F
print("Favorite emoji c is {0:c}".format(c))

print("Favorite s is {0:s}".format(favorite))
print("Favorite .2s is {0:.2s}".format(favorite))
print("Favorite 11s is     |{0:11s}|".format(favorite))
print("Favorite 11.2s is   |{0:11.2s}|".format(favorite))
print("Favorite <11.2s is  |{0:<11.2s}|".format(favorite))
print("Favorite ^11.2s is  |{0:^11.2s}|".format(favorite))
print("Favorite >11.2s is  |{0:>11.2s}|".format(favorite))
print("Favorite .<11.2s is |{0:.<11.2s}|".format(favorite))
print("Favorite *^11.2s is |{0:*^11.2s}|".format(favorite))
print("Favorite ->11.2s is |{0:->11.2s}|".format(favorite))

print("Favorite d is {0:d}".format(i1))
print("Another d is {0:d}".format(i2))
print("Favorite b is {0:b}".format(i1))
print("Another B is {0:b}".format(i2))
print("Favorite o is {0:o}".format(i1))
print("Another o is {0:o}".format(i2))
print("Favorite x is {0:x}".format(i1))
print("Another X is {0:X}".format(i2))
print("Favorite #b is {0:#b}".format(i1))
print("Another #B is {0:#b}".format(i2))
print("Favorite #o is {0:#o}".format(i1))
print("Another #o is {0:#o}".format(i2))
print("Favorite #x is {0:#x}".format(i1))
print("Another #X is {0:#X}".format(i2))
print("Favorite 11d is   |{0:11d}|".format(i1))
print("Favorite +11d is  |{0:+11d}|".format(i1))
print("Favorite 011d is  |{0:011d}|".format(i1))
print("Favorite 011x is  |{0:011x}|".format(i1))
print("Favorite #011x is |{0:#011x}|".format(i1))

print("Favorite f is {0:f}".format(fp1))
print("Another f is {0:f}".format(fp2))
print("One more f is {0:f}".format(fp3))
print("Favorite e is {0:e}".format(fp1))
print("Another e is {0:e}".format(fp2))
print("One more e is {0:e}".format(fp3))
print("Favorite g is {0:g}".format(fp1))
print("Another g is {0:g}".format(fp2))
print("One more g is {0:g}".format(fp3))
print("Favorite .2f is {0:.2f}".format(fp1))
print("Another .2f is {0:.2f}".format(fp2))
print("One more .2f is {0:.2f}".format(fp3))
print("Favorite .2e is {0:.2e}".format(fp1))
print("Another .2e is {0:.2e}".format(fp2))
print("One more .2e is {0:.2e}".format(fp3))
print("Favorite .2g is {0:.2g}".format(fp1))
print("Another .2g is {0:.2g}".format(fp2))
print("One more .2g is {0:.2g}".format(fp3))
print("Favorite 15.2f is |{0:15.2f}|".format(fp1))
print("Another 15.2e is  |{0:15.2e}|".format(fp2))
print("One more 15.2g is |{0:15.2g}|".format(fp3))

Output
$ python3 OutputFormatting4.py Last, First Middle Left Center Right Favorite number is 3261963 Favorite FP is 3.1415926 Favorite c is k Favorite 11c is | k| Favorite <11c is |k | Favorite ^11c is | k | Favorite >11c is | k| Favorite .<11c is |k..........| Favorite _^11c is |_____k_____| Favorite >11c is | k| Favorite emoji c is 🤯 Favorite s is kerfuffle Favorite .2s is ke Favorite 11s is |kerfuffle | Favorite 11.2s is |ke | Favorite <11.2s is |ke | Favorite ^11.2s is | ke | Favorite >11.2s is | ke| Favorite .<11.2s is |ke.........| Favorite *^11.2s is |****ke*****| Favorite ->11.2s is |---------ke| Favorite d is 3261963 Another d is -42 Favorite b is 1100011100011000001011 Another B is -101010 Favorite o is 14343013 Another o is -52 Favorite x is 31c60b Another X is -2A Favorite #b is 0b1100011100011000001011 Another #B is -0b101010 Favorite #o is 0o14343013 Another #o is -0o52 Favorite #x is 0x31c60b Another #X is -0X2A Favorite 11d is | 3261963| Favorite +11d is | +3261963| Favorite 011d is |00003261963| Favorite 011x is |0000031c60b| Favorite #011x is |0x00031c60b| Favorite f is 3.141593 Another f is 2997924580.000000 One more f is -0.000123 Favorite e is 3.141593e+00 Another e is 2.997925e+09 One more e is -1.234000e-04 Favorite g is 3.14159 Another g is 2.99792e+09 One more g is -0.0001234 Favorite .2f is 3.14 Another .2f is 2997924580.00 One more .2f is -0.00 Favorite .2e is 3.14e+00 Another .2e is 3.00e+09 One more .2e is -1.23e-04 Favorite .2g is 3.1 Another .2g is 3e+09 One more .2g is -0.00012 Favorite 15.2f is | 3.14| Another 15.2e is | 3.00e+09| One more 15.2g is | -0.00012|

Questions

Projects

More ★'s indicate higher difficulty level.

References