Skip to content

Commit 00f19ef

Browse files
committed
Issue #25179: Preparatory cleanup of existing docs on string formatting
* There was a link pointing to the section on the string.Formatter class (and multiple links in Python 3), when the section on the common format string syntax is probably more appropriate * Fix references to various format() functions and methods * Nested replacement fields may contain conversions and format specifiers, and this is tested in Python 3; see Issue #19729 for instance
1 parent 9c61f2e commit 00f19ef

5 files changed

Lines changed: 21 additions & 19 deletions

File tree

Doc/library/datetime.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ Instance methods:
558558

559559
.. method:: date.__format__(format)
560560

561-
Same as :meth:`.date.strftime`. This makes it possible to specify format
561+
Same as :meth:`.date.strftime`. This makes it possible to specify a format
562562
string for a :class:`.date` object when using :meth:`str.format`.
563563
See section :ref:`strftime-strptime-behavior`.
564564

@@ -1058,7 +1058,7 @@ Instance methods:
10581058

10591059
.. method:: datetime.__format__(format)
10601060

1061-
Same as :meth:`.datetime.strftime`. This makes it possible to specify format
1061+
Same as :meth:`.datetime.strftime`. This makes it possible to specify a format
10621062
string for a :class:`.datetime` object when using :meth:`str.format`.
10631063
See section :ref:`strftime-strptime-behavior`.
10641064

@@ -1292,7 +1292,7 @@ Instance methods:
12921292

12931293
.. method:: time.__format__(format)
12941294

1295-
Same as :meth:`.time.strftime`. This makes it possible to specify format string
1295+
Same as :meth:`.time.strftime`. This makes it possible to specify a format string
12961296
for a :class:`.time` object when using :meth:`str.format`.
12971297
See section :ref:`strftime-strptime-behavior`.
12981298

Doc/library/pprint.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ are converted to strings. The default implementation uses the internals of the
190190
the current presentation context (direct and indirect containers for *object*
191191
that are affecting the presentation) as the keys; if an object needs to be
192192
presented which is already represented in *context*, the third return value
193-
should be ``True``. Recursive calls to the :meth:`format` method should add
193+
should be ``True``. Recursive calls to the :meth:`.format` method should add
194194
additional entries for containers to this dictionary. The third argument,
195195
*maxlevels*, gives the requested limit to recursion; this will be ``0`` if there
196196
is no requested limit. This argument should be passed unmodified to recursive

Doc/library/string.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ The constants defined in this module are:
105105

106106
.. _new-string-formatting:
107107

108-
String Formatting
109-
-----------------
108+
Custom String Formatting
109+
------------------------
110110

111111
.. versionadded:: 2.6
112112

@@ -115,17 +115,17 @@ to do complex variable substitutions and value formatting via the
115115
:meth:`str.format` method described in :pep:`3101`. The :class:`Formatter`
116116
class in the :mod:`string` module allows you to create and customize your own
117117
string formatting behaviors using the same implementation as the built-in
118-
:meth:`format` method.
118+
:meth:`~str.format` method.
119119

120120
.. class:: Formatter
121121

122122
The :class:`Formatter` class has the following public methods:
123123

124124
.. method:: format(format_string, *args, **kwargs)
125125

126-
:meth:`format` is the primary API method. It takes a format string and
126+
The primary API method. It takes a format string and
127127
an arbitrary set of positional and keyword arguments.
128-
:meth:`format` is just a wrapper that calls :meth:`vformat`.
128+
It is just a wrapper that calls :meth:`vformat`.
129129

130130
.. method:: vformat(format_string, args, kwargs)
131131

@@ -293,8 +293,9 @@ Most built-in types support a common formatting mini-language, which is
293293
described in the next section.
294294

295295
A *format_spec* field can also include nested replacement fields within it.
296-
These nested replacement fields can contain only a field name; conversion flags
297-
and format specifications are not allowed. The replacement fields within the
296+
These nested replacement fields may contain a field name, conversion flag
297+
and format specification, but deeper nesting is
298+
not allowed. The replacement fields within the
298299
format_spec are substituted before the *format_spec* string is interpreted.
299300
This allows the formatting of a value to be dynamically specified.
300301

@@ -332,8 +333,10 @@ The general form of a *standard format specifier* is:
332333

333334
If a valid *align* value is specified, it can be preceded by a *fill*
334335
character that can be any character and defaults to a space if omitted.
335-
Note that it is not possible to use ``{`` and ``}`` as *fill* char while
336-
using the :meth:`str.format` method; this limitation however doesn't
336+
It is not possible to use a literal curly brace ("``{``" or "``}``") as
337+
the *fill* character when using the :meth:`str.format`
338+
method. However, it is possible to insert a curly brace
339+
with a nested replacement field. This limitation doesn't
337340
affect the :func:`format` function.
338341

339342
The meaning of the various alignment options is as follows:
@@ -508,8 +511,8 @@ The available presentation types for floating point and decimal values are:
508511
Format examples
509512
^^^^^^^^^^^^^^^
510513

511-
This section contains examples of the new format syntax and comparison with
512-
the old ``%``-formatting.
514+
This section contains examples of the :meth:`str.format` syntax and
515+
comparison with the old ``%``-formatting.
513516

514517
In most of the cases the syntax is similar to the old ``%``-formatting, with the
515518
addition of the ``{}`` and with ``:`` used instead of ``%``.

Doc/tools/susp-ignored.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ howto/pyporting,,::,Programming Language :: Python :: 2
4646
howto/pyporting,,::,Programming Language :: Python :: 3
4747
howto/regex,,::,
4848
howto/regex,,:foo,(?:foo)
49-
howto/urllib2,,:example,"for example ""joe@password:example.com"""
49+
howto/urllib2,,:password,"for example ""joe:password@example.com"""
5050
library/audioop,,:ipos,"# factor = audioop.findfactor(in_test[ipos*2:ipos*2+len(out_test)],"
5151
library/bisect,,:hi,all(val >= x for val in a[i:hi])
5252
library/bisect,,:hi,all(val > x for val in a[i:hi])

Doc/tutorial/introduction.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,8 @@ The built-in function :func:`len` returns the length of a string::
357357
Both strings and Unicode strings support a large number of methods for
358358
basic transformations and searching.
359359

360-
:ref:`new-string-formatting`
361-
Information about string formatting with :meth:`str.format` is described
362-
here.
360+
:ref:`formatstrings`
361+
Information about string formatting with :meth:`str.format`.
363362

364363
:ref:`string-formatting`
365364
The old formatting operations invoked when strings and Unicode strings are

0 commit comments

Comments
 (0)