Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 11ca1d9

Browse filesBrowse files
miss-islingtonblaisepuatach
authored
[3.13] GH-110383: Improve Tutorial for Input Ouput (GH-119230) (GH-119326)
GH-110383: Improve Tutorial for Input Ouput (GH-119230) (cherry picked from commit 9db2fd7) Co-authored-by: Blaise Pabon <blaise@gmail.com> Co-authored-by: edson duarte <eduarte.uatach@gmail.com>
1 parent d93c4f9 commit 11ca1d9
Copy full SHA for 11ca1d9

File tree

Expand file treeCollapse file tree

1 file changed

+16
-4
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-4
lines changed

‎Doc/tutorial/inputoutput.rst

Copy file name to clipboardExpand all lines: Doc/tutorial/inputoutput.rst
+16-4Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +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+
>>> total_votes = 85_705_149
48+
>>> percentage = yes_votes / total_votes
4749
>>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
4850
' 42572654 YES votes 49.67%'
4951

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+
5057
* Finally, you can do all the string handling yourself by using string slicing and
5158
concatenation operations to create any layout you can imagine. The
5259
string type has some methods that perform useful operations for padding
@@ -197,7 +204,12 @@ notation. ::
197204
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
198205

199206
This is particularly useful in combination with the built-in function
200-
:func:`vars`, which returns a dictionary containing all local variables.
207+
:func:`vars`, which returns a dictionary containing all local variables::
208+
209+
>>> table = {k: str(v) for k, v in vars().items()}
210+
>>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()])
211+
>>> print(message.format(**table))
212+
__name__: __main__; __doc__: None; __package__: None; __loader__: ...
201213

202214
As an example, the following lines produce a tidily aligned
203215
set of columns giving integers and their squares and cubes::

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.