Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
254c17c7
Commit
254c17c7
authored
Sep 01, 2009
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#6813: better documentation for numberless string formats.
parent
90161375
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
5 deletions
+14
-5
Doc/library/string.rst
Doc/library/string.rst
+3
-3
Doc/tutorial/inputoutput.rst
Doc/tutorial/inputoutput.rst
+11
-2
No files found.
Doc/library/string.rst
View file @
254c17c7
...
@@ -220,7 +220,7 @@ literal text, it can be escaped by doubling: ``{{`` and ``}}``.
...
@@ -220,7 +220,7 @@ literal text, it can be escaped by doubling: ``{{`` and ``}}``.
The grammar for a replacement field is as follows:
The grammar for a replacement field is as follows:
.. productionlist:: sf
.. productionlist:: sf
replacement_field: "{"
`field_name`
["!" `conversion`] [":" `format_spec`] "}"
replacement_field: "{"
[`field_name`]
["!" `conversion`] [":" `format_spec`] "}"
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")*
arg_name: (`identifier` | `integer`)?
arg_name: (`identifier` | `integer`)?
attribute_name: `identifier`
attribute_name: `identifier`
...
@@ -228,7 +228,7 @@ The grammar for a replacement field is as follows:
...
@@ -228,7 +228,7 @@ The grammar for a replacement field is as follows:
conversion: "r" | "s"
conversion: "r" | "s"
format_spec: <described in the next section>
format_spec: <described in the next section>
In less formal terms, the replacement field
starts
with a *field_name* that specifies
In less formal terms, the replacement field
can start
with a *field_name* that specifies
the object whose value is to be formatted and inserted
the object whose value is to be formatted and inserted
into the output instead of the replacement field.
into the output instead of the replacement field.
The *field_name* is optionally followed by a *conversion* field, which is
The *field_name* is optionally followed by a *conversion* field, which is
...
@@ -249,7 +249,7 @@ Some simple format string examples::
...
@@ -249,7 +249,7 @@ Some simple format string examples::
"First, thou shalt count to {0}" # References first positional argument
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0
]
to {1}"
"From {} to {}" # Same as "From {0
}
to {1}"
"My quest is {name}" # References keyword argument 'name'
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
...
...
Doc/tutorial/inputoutput.rst
View file @
254c17c7
...
@@ -123,11 +123,11 @@ with zeros. It understands about plus and minus signs::
...
@@ -123,11 +123,11 @@ with zeros. It understands about plus and minus signs::
Basic usage of the :meth:`str.format` method looks like this::
Basic usage of the :meth:`str.format` method looks like this::
>>> print 'We are the {
0} who say "{1
}!"'.format('knights', 'Ni')
>>> print 'We are the {
} who say "{
}!"'.format('knights', 'Ni')
We are the knights who say "Ni!"
We are the knights who say "Ni!"
The brackets and characters within them (called format fields) are replaced with
The brackets and characters within them (called format fields) are replaced with
the objects passed into the :meth:`~str.format` method.
The
number in the
the objects passed into the :meth:`~str.format` method.
A
number in the
brackets refers to the position of the object passed into the
brackets refers to the position of the object passed into the
:meth:`~str.format` method. ::
:meth:`~str.format` method. ::
...
@@ -149,6 +149,15 @@ Positional and keyword arguments can be arbitrarily combined::
...
@@ -149,6 +149,15 @@ Positional and keyword arguments can be arbitrarily combined::
... other='Georg')
... other='Georg')
The story of Bill, Manfred, and Georg.
The story of Bill, Manfred, and Georg.
``'!s'`` (apply :func:`str`) and ``'!r'`` (apply :func:`repr`) can be used to
convert the value before it is formatted. ::
>>> import math
>>> print 'The value of PI is approximately {}.'.format(math.pi)
The value of PI is approximately 3.14159265359.
>>> print 'The value of PI is approximately {!r}.'.format(math.pi)
The value of PI is approximately 3.141592653589793.
An optional ``':'`` and format specifier can follow the field name. This allows
An optional ``':'`` and format specifier can follow the field name. This allows
greater control over how the value is formatted. The following example
greater control over how the value is formatted. The following example
truncates Pi to three places after the decimal.
truncates Pi to three places after the decimal.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment