@@ -37,18 +37,23 @@ printing space-separated values. There are several ways to format output.
3737* The :meth: `str.format ` method of strings requires more manual
3838 effort. You'll still use ``{ `` and ``} `` to mark where a variable
3939 will be substituted and can provide detailed formatting directives,
40- but you'll also need to provide the information to be formatted.
40+ but you'll also need to provide the information to be formatted. In the following code
41+ block there are two examples of how to format variables:
42+
4143
4244 ::
4345
4446 >>> yes_votes = 42_572_654
45- >>> no_votes = 43_132_495
46- >>> percentage = yes_votes / (yes_votes + no_votes)
47- >>> # Print yes_votes padded with spaces and a negative sign only for negative numbers
48- >>> # Also print percentage multiplied by 100, with 2 decimal places and followed by a percent sign:
47+ >>> total_votes = 43_132_495
48+ >>> percentage = yes_votes / (yes_votes + total_votes)
4949 >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
5050 ' 42572654 YES votes 49.67%'
5151
52+ Notice how the ``yes_votes `` are padded with spaces and a negative sign only for negative numbers.
53+ The example also prints ``percentage `` multiplied by 100, with 2 decimal
54+ places and followed by a percent sign (see :ref: `formatspec ` for details).
55+
56+
5257* Finally, you can do all the string handling yourself by using string slicing and
5358 concatenation operations to create any layout you can imagine. The
5459 string type has some methods that perform useful operations for padding
@@ -204,7 +209,7 @@ This is particularly useful in combination with the built-in function
204209 >>> table = {k: str(v) for k, v in vars().items()}
205210 >>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()])
206211 >>> print(message.format(**table))
207- __name__: __main__; __doc__: None; __package__: None; __loader__: <class '_frozen_importlib.BuiltinImporter'>; __spec__: None; __annotations__: {}; __builtins__: <module 'builtins' (built-in)>;
212+ __name__: __main__; __doc__: None; __package__: None; __loader__: ...
208213
209214As an example, the following lines produce a tidily aligned
210215set of columns giving integers and their squares and cubes::
0 commit comments