Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f3ed68

Browse files
committed
Recorded merge of revisions 74614 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r74614 | georg.brandl | 2009-09-01 09:40:54 +0200 (Di, 01 Sep 2009) | 1 line #6813: better documentation for numberless string formats. ........
1 parent c9a5a0e commit 2f3ed68

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

Doc/library/string.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,15 @@ literal text, it can be escaped by doubling: ``{{`` and ``}}``.
194194
The grammar for a replacement field is as follows:
195195

196196
.. productionlist:: sf
197-
replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}"
197+
replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}"
198198
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
199199
arg_name: (`identifier` | `integer`)?
200200
attribute_name: `identifier`
201201
element_index: `integer`
202202
conversion: "r" | "s" | "a"
203203
format_spec: <described in the next section>
204204

205-
In less formal terms, the replacement field starts with a *field_name* that specifies
205+
In less formal terms, the replacement field can start with a *field_name* that specifies
206206
the object whose value is to be formatted and inserted
207207
into the output instead of the replacement field.
208208
The *field_name* is optionally followed by a *conversion* field, which is
@@ -223,7 +223,7 @@ Some simple format string examples::
223223

224224
"First, thou shalt count to {0}" # References first positional argument
225225
"Bring me a {}" # Implicitly references the first positional argument
226-
"From {} to {}" # Same as "From {0] to {1}"
226+
"From {} to {}" # Same as "From {0} to {1}"
227227
"My quest is {name}" # References keyword argument 'name'
228228
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
229229
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
@@ -243,6 +243,7 @@ Some examples::
243243

244244
"Harold's a clever {0!s}" # Calls str() on the argument first
245245
"Bring out the holy {name!r}" # Calls repr() on the argument first
246+
"More {!a}" # Calls ascii() on the argument first
246247

247248
The *format_spec* field contains a specification of how the value should be
248249
presented, including such details as field width, alignment, padding, decimal

Doc/tutorial/inputoutput.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ with zeros. It understands about plus and minus signs::
126126

127127
Basic usage of the :meth:`str.format` method looks like this::
128128

129-
>>> print('We are the {0} who say "{1}!"'.format('knights', 'Ni'))
129+
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
130130
We are the knights who say "Ni!"
131131

132132
The brackets and characters within them (called format fields) are replaced with
133-
the objects passed into the :meth:`~str.format` method. The number in the
134-
brackets refers to the position of the object passed into the
133+
the objects passed into the :meth:`~str.format` method. A number in the
134+
brackets can be used to refer to the position of the object passed into the
135135
:meth:`~str.format` method. ::
136136

137137
>>> print('{0} and {1}'.format('spam', 'eggs'))
@@ -152,6 +152,15 @@ Positional and keyword arguments can be arbitrarily combined::
152152
other='Georg'))
153153
The story of Bill, Manfred, and Georg.
154154

155+
``'!a'`` (apply :func:`ascii`), ``'!s'`` (apply :func:`str`) and ``'!r'``
156+
(apply :func:`repr`) can be used to convert the value before it is formatted::
157+
158+
>>> import math
159+
>>> print('The value of PI is approximately {}.'.format(math.pi))
160+
The value of PI is approximately 3.14159265359.
161+
>>> print('The value of PI is approximately {!r}.'.format(math.pi))
162+
The value of PI is approximately 3.141592653589793.
163+
155164
An optional ``':'`` and format specifier can follow the field name. This allows
156165
greater control over how the value is formatted. The following example
157166
truncates Pi to three places after the decimal.

0 commit comments

Comments
 (0)