From 5f5ae362c5df9d0935d62ae99370bf2bfea58c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 18 Feb 2019 10:16:17 +0100 Subject: [PATCH 1/3] bpo-35126: Fix a mistake in FAQ about converting number to string --- Doc/faq/programming.rst | 24 ++++++++++++++++--- .../2019-02-18-10-01-07.bpo-35126.LWwl8X.rst | 2 ++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Documentation/2019-02-18-10-01-07.bpo-35126.LWwl8X.rst diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 9190e1a8e959e4..ba0cedcccbcb89 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -997,9 +997,27 @@ To convert, e.g., the number 144 to the string '144', use the built-in type constructor :func:`str`. If you want a hexadecimal or octal representation, use the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields -``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``. You may also use -:ref:`the % operator ` on strings. See the library reference -manual for details. +``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``. In Python 2, the +division (/) operator returns the floor of the mathematical result of division +if the arguments are ints or longs, but it returns a reasonable approximation of +the division result if the arguments are floats or complex:: + + >>> print('{:.3f}'.format(1/3)) + 0.000 + >>> print('{:.3f}'.format(1.0/3)) + 0.333 + +In Python 3, the default behaviour of the division operator (see :pep:`238`) has +been changed but you can have the same behaviour in Python 2 if you import +`division` from :mod:`__future__`:: + + >>> from __future__ import division + >>> print('{:.3f}'.format(1/3)) + 0.333 + + +You may also use :ref:`the % operator ` on strings. See the +library reference manual for details. How do I modify a string in place? diff --git a/Misc/NEWS.d/next/Documentation/2019-02-18-10-01-07.bpo-35126.LWwl8X.rst b/Misc/NEWS.d/next/Documentation/2019-02-18-10-01-07.bpo-35126.LWwl8X.rst new file mode 100644 index 00000000000000..8ad392ee3c97e9 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2019-02-18-10-01-07.bpo-35126.LWwl8X.rst @@ -0,0 +1,2 @@ +Improve the examples in the "How do I convert a number to string?" question +of the "Programming" section of the FAQ. Contributed by Stéphane Wirtel. From 4144b2527b16e62bd24290d6ccdc3d8855391d78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Mon, 18 Feb 2019 10:36:12 +0100 Subject: [PATCH 2/3] Fix an error with rstlint --- Doc/faq/programming.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index ba0cedcccbcb89..e1a910a8c2f9fd 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1009,7 +1009,7 @@ the division result if the arguments are floats or complex:: In Python 3, the default behaviour of the division operator (see :pep:`238`) has been changed but you can have the same behaviour in Python 2 if you import -`division` from :mod:`__future__`:: +``division`` from :mod:`__future__` :: >>> from __future__ import division >>> print('{:.3f}'.format(1/3)) From 7b3e5113d45b6af5dde13ecee2bc53b076c85aef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Tue, 19 Feb 2019 09:22:25 +0100 Subject: [PATCH 3/3] Remove the space before :: --- Doc/faq/programming.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index e1a910a8c2f9fd..62b34b6e68ef5f 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1009,7 +1009,7 @@ the division result if the arguments are floats or complex:: In Python 3, the default behaviour of the division operator (see :pep:`238`) has been changed but you can have the same behaviour in Python 2 if you import -``division`` from :mod:`__future__` :: +``division`` from :mod:`__future__`:: >>> from __future__ import division >>> print('{:.3f}'.format(1/3))