Index: Doc/tutorial/inputoutput.rst =================================================================== --- Doc/tutorial/inputoutput.rst (revision 74612) +++ Doc/tutorial/inputoutput.rst (working copy) @@ -126,12 +126,12 @@ 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!" 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 -brackets refers to the position of the object passed into the +the objects passed into the :meth:`~str.format` method. Numbers in the +brackets can be used to refer to the position of the object passed into the :meth:`~str.format` method. :: >>> print('{0} and {1}'.format('spam', 'eggs')) @@ -152,6 +152,16 @@ other='Georg')) The story of Bill, Manfred, and Georg. +An optional ``'!'`` can be used to convert the value before it is represented. +The options of ``'!a'`` (:func:`ascii`), ``'!s'`` (:func:`str`) and ``'!r'`` +(:func:`repr`) can be used. :: + + >>> 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 greater control over how the value is formatted. The following example truncates Pi to three places after the decimal. Index: Doc/library/string.rst =================================================================== --- Doc/library/string.rst (revision 74612) +++ Doc/library/string.rst (working copy) @@ -194,7 +194,7 @@ The grammar for a replacement field is as follows: .. productionlist:: sf - replacement_field: "{" `field_name` ["!" `conversion`] [":" `format_spec`] "}" + replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}" field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")* arg_name: (`identifier` | `integer`)? attribute_name: `identifier` @@ -202,7 +202,7 @@ conversion: "r" | "s" | "a" format_spec: -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 into the output instead of the replacement field. The *field_name* is optionally followed by a *conversion* field, which is @@ -223,7 +223,7 @@ "First, thou shalt count to {0}" # References 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' "Weight in tons {0.weight}" # 'weight' attribute of first positional arg "Units destroyed: {players[0]}" # First element of keyword argument 'players'. @@ -243,6 +243,7 @@ "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first + "More {!a}" # Calls ascii() on the argument first The *format_spec* field contains a specification of how the value should be presented, including such details as field width, alignment, padding, decimal