11. Formatting Strings - Old Style#
So far, we’ve been using string concatenation and multiple arguments to print()
to format strings. In this notebook, we will use printf style formatting to put values into strings with a variety of different formats.
Python offers three different ways to format strings:
old style (all versions of Pythons) - this notebook
new style (Python 2.6 +)
f-strings (Python 3.6 +)
In this notebook, we are directly printing the results of the formatting, but we can also assign the results to a variable or use as an expression (e.g., as an argument to a function).
11.1. Old style: %#
This format uses the form of format_string % data. Within each string are one of more placeholder fields that will be replaced by values after the %
operator.
Note: The book Introducing Python does considers this formatting to be string interpolation as the format strings contain placeholders. Others strictly require expressions to be supported to be considered true string interpoloation (which exists in f-strings).
While development and support of Python 2 has ended, the old style formatting will remain in Python for the forseeable future.
Placeholders follow this format: %[flags][minWidth][.maxchars]type
The square brackets indicate an optional component.
Type |
Description |
---|---|
%s |
string |
%d |
integer |
%x |
hexadecimal integer |
%o |
octal integer |
%f |
float |
%e |
float in exponential format |
%g |
decimal or exponential float |
%% |
the character % |
For the optional components -
The flags have the possible options
-
left aligns the output of the placeholder.+
Prepends a plus for positive signed-numeric types0
will prepend zeros for numeric types if the width is present
The minwidth is the minimum size of the placeholder.
If the type is a string, .maxchars specifies how many characters to print from the data value.
If the type is a float, .maxchars specifies the precision - the number of digits after the decimal point.
If the type is an integer, .maxchars specifies the length of the integer to print. zeros will be prepended if the number is less than that length.
1alphabet = "abcdefghijklmnopqrstuvwxyz"
2print("123456789012345678901234567890")
3print("%s" % alphabet)
4print("%.5s" % alphabet)
5print("%30.5s" % alphabet)
6print("%-30.5s" % alphabet)
123456789012345678901234567890
abcdefghijklmnopqrstuvwxyz
abcde
abcde
abcde
1year = 1892
2print("123456789012345678901234567890")
3print("%d" % year)
4print("%20d" % year)
5print("%+20d" % year)
6print("%020d" % year)
7print("%20.8d" % year)
8print("%-20d" % year)
9print("%x" % year)
10print("%o" % year)
123456789012345678901234567890
1892
1892
+1892
00000000000000001892
00001892
1892
764
3544
1gain = 64.98712
2print("123456789012345678901234567890")
3print("%f" % gain)
4print("%20f" % gain)
5print("%+20f" % gain)
6print("%020f" % gain)
7print("%20.8f" % gain)
8print("%20.2f" % gain)
9print("%-20f" % gain)
123456789012345678901234567890
64.987120
64.987120
+64.987120
0000000000064.987120
64.98712000
64.99
64.987120
1gain = 604132421.988
2print("123456789012345678901234567890")
3print("%e" % gain)
4print("%20e" % gain)
5print("%+20e" % gain)
6print("%020e" % gain)
7print("%20.8e" % gain)
8print("%20.2e" % gain)
9print("%-20e" % gain)
10print("%20.12e" % gain)
123456789012345678901234567890
6.041324e+08
6.041324e+08
+6.041324e+08
000000006.041324e+08
6.04132422e+08
6.04e+08
6.041324e+08
6.041324219880e+08
Of course, we can include other content within these strings
1print("In %d, Duke University moved to Durham, North Carolina." % 1892)
In 1892, Duke University moved to Durham, North Carolina.
You can also specify multiple placeholders within a string. The values will put inside of a tuple (which you will see in Notebook 3d). For right now, you just need to know that tuples are bound by paranthesis with values separated inside by commas
1year = 1838
2school = "Duke University"
3city = "Trinity, North Carolina"
4print("In %d, %s was founded in %s." % (year,school,city))
5year = 1842
6school = "the University of Notre Dame"
7city = "South Bend, Indiana"
8print("In %d, %s was founded in %s." % (year,school,city))
In 1838, Duke University was founded in Trinity, North Carolina.
In 1842, the University of Notre Dame was founded in South Bend, Indiana.