8000 [2.7] bpo-35126: Fix a mistake in FAQ about converting number to stri… · python/cpython@c611db4 · GitHub
[go: up one dir, main page]

Skip to content

Commit c611db4

Browse files< 8000 /div>
matrixisemiss-islington
authored andcommitted
[2.7] bpo-35126: Fix a mistake in FAQ about converting number to string (GH-11911)
https://bugs.python.org/issue35126
1 parent d5409eb commit c611db4

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Doc/faq/programming.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,27 @@ To convert, e.g., the number 144 to the string '144', use the built-in type
997997
constructor :func:`str`. If you want a hexadecimal or octal representation, use
998998
the built-in functions :func:`hex` or :func:`oct`. For fancy formatting, see
999999
the :ref:`formatstrings` section, e.g. ``"{:04d}".format(144)`` yields
1000-
``'0144'`` and ``"{:.3f}".format(1/3)`` yields ``'0.333'``. You may also use
1001-
:ref:`the % operator <string-formatting>` on strings. See the library reference
1002-
manual for details.
1000+
``'0144'`` and ``"{:.3f}".format(1.0/3.0)`` yields ``'0.333'``. In Python 2, the
1001+
division (/) operator returns the floor of the mathematical result of division
1002+
if the arguments are ints or longs, but it returns a reasonable approximation of
1003+
the division result if the arguments are floats or complex::
1004+
1005+
>>> print('{:.3f}'.format(1/3))
1006+
0.000
1007+
>>> print('{:.3f}'.format(1.0/3))
1008+
0.333
1009+
1010+
In Python 3, the default behaviour of the division operator (see :pep:`238`) has
1011+
been changed but you can have the same behaviour in Python 2 if you import
1012+
``division`` from :mod:`__future__`::
1013+
1014+
>>> from __future__ import division
1015+
>>> print('{:.3f}'.format(1/3))
1016+
0.333
1017+
1018+
1019+
You may also use :ref:`the % operator <string-formatting>` on strings. See the
1020+
library reference manual for details.
10031021

10041022

10051023
How do I modify a string in place?
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve the examples in the "How do I convert a number to string?" question
2+
of the "Programming" section of the FAQ. Contributed by Stéphane Wirtel.

0 commit comments

Comments
 (0)
0