From eb04e279fcbf856d5f4c907fbd57e575d954175f Mon Sep 17 00:00:00 2001 From: edson duarte Date: Sun, 12 May 2024 11:24:29 -0300 Subject: [PATCH 1/5] Add explanation to formats in 7.1 --- Doc/tutorial/inputoutput.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index fe9ca9ccb9c7e0..ab11b90d251eae 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -44,6 +44,8 @@ printing space-separated values. There are several ways to format output. >>> yes_votes = 42_572_654 >>> no_votes = 43_132_495 >>> percentage = yes_votes / (yes_votes + no_votes) + >>> # Print yes_votes padded with spaces and a negative sign only for negative numbers + >>> # Also print percentage multiplied by 100, with 2 decimal places and followed by a percent sign: >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage) ' 42572654 YES votes 49.67%' From 2166f581a2810dd76e662112405821a56c9dd0cc Mon Sep 17 00:00:00 2001 From: edson duarte Date: Sun, 12 May 2024 11:29:22 -0300 Subject: [PATCH 2/5] Add example using built-in vars in 7.1.2 --- Doc/tutorial/inputoutput.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index ab11b90d251eae..059ea7882977f2 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -199,7 +199,12 @@ notation. :: Jack: 4098; Sjoerd: 4127; Dcab: 8637678 This is particularly useful in combination with the built-in function -:func:`vars`, which returns a dictionary containing all local variables. +:func:`vars`, which returns a dictionary containing all local variables. :: + + >>> table = {k: str(v) for k, v in vars().items()} + >>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()]) + >>> print(message.format(**table)) + __name__: __main__; __doc__: None; __package__: None; __loader__: ; __spec__: None; __annotations__: {}; __builtins__: ; As an example, the following lines produce a tidily aligned set of columns giving integers and their squares and cubes:: From 4e3a051ac03365afb386bc3ce41c1ff1df61ad2d Mon Sep 17 00:00:00 2001 From: blaisep Date: Mon, 20 May 2024 12:06:12 -0400 Subject: [PATCH 3/5] Update PR GH-118970 with some format changes I made some formatting changes to some examples in PR https://github.com/python/cpython/pull/118970 from @uatach The idea was to get this merged quickly. --- Doc/tutorial/inputoutput.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 059ea7882977f2..9096a6c8e96d09 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -37,18 +37,23 @@ printing space-separated values. There are several ways to format output. * The :meth:`str.format` method of strings requires more manual effort. You'll still use ``{`` and ``}`` to mark where a variable will be substituted and can provide detailed formatting directives, - but you'll also need to provide the information to be formatted. + but you'll also need to provide the information to be formatted. In the following code + block there are two examples of how to format variables: + :: >>> yes_votes = 42_572_654 - >>> no_votes = 43_132_495 - >>> percentage = yes_votes / (yes_votes + no_votes) - >>> # Print yes_votes padded with spaces and a negative sign only for negative numbers - >>> # Also print percentage multiplied by 100, with 2 decimal places and followed by a percent sign: + >>> total_votes = 43_132_495 + >>> percentage = yes_votes / (yes_votes + total_votes) >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage) ' 42572654 YES votes 49.67%' + Notice how the ``yes_votes`` are padded with spaces and a negative sign only for negative numbers. + The example also prints ``percentage`` multiplied by 100, with 2 decimal + places and followed by a percent sign (see :ref:`formatspec` for details). + + * Finally, you can do all the string handling yourself by using string slicing and concatenation operations to create any layout you can imagine. The 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 >>> table = {k: str(v) for k, v in vars().items()} >>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()]) >>> print(message.format(**table)) - __name__: __main__; __doc__: None; __package__: None; __loader__: ; __spec__: None; __annotations__: {}; __builtins__: ; + __name__: __main__; __doc__: None; __package__: None; __loader__: ... As an example, the following lines produce a tidily aligned set of columns giving integers and their squares and cubes:: From 04440148ed1471ddf81b5b08801ada86e6660db5 Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Mon, 20 May 2024 21:35:53 +0200 Subject: [PATCH 4/5] Update Doc/tutorial/inputoutput.rst --- Doc/tutorial/inputoutput.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 9096a6c8e96d09..37b415cbd8b122 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -204,7 +204,7 @@ notation. :: Jack: 4098; Sjoerd: 4127; Dcab: 8637678 This is particularly useful in combination with the built-in function -:func:`vars`, which returns a dictionary containing all local variables. :: +:func:`vars`, which returns a dictionary containing all local variables:: >>> table = {k: str(v) for k, v in vars().items()} >>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()]) From 49a5778df5980594b2bbc1c648fb19f8cc50fdd0 Mon Sep 17 00:00:00 2001 From: Blaise Pabon Date: Tue, 21 May 2024 10:49:02 -0400 Subject: [PATCH 5/5] Update Doc/tutorial/inputoutput.rst Co-authored-by: Petr Viktorin --- Doc/tutorial/inputoutput.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index 37b415cbd8b122..857068a51ab843 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -44,8 +44,8 @@ printing space-separated values. There are several ways to format output. :: >>> yes_votes = 42_572_654 - >>> total_votes = 43_132_495 - >>> percentage = yes_votes / (yes_votes + total_votes) + >>> total_votes = 85_705_149 + >>> percentage = yes_votes / total_votes >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage) ' 42572654 YES votes 49.67%'