diff options
Diffstat (limited to 'Doc/library/string.rst')
-rw-r--r-- | Doc/library/string.rst | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/Doc/library/string.rst b/Doc/library/string.rst index a87d285..674b48b 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -192,6 +192,15 @@ subclasses can define their own format string syntax). The syntax is related to that of :ref:`formatted string literals <f-strings>`, but there are differences. +.. index:: + single: {; in string formatting + single: }; in string formatting + single: .; in string formatting + single: [; in string formatting + single: ]; in string formatting + single: !; in string formatting + single: :; in string formatting + Format strings contain "replacement fields" surrounded by curly braces ``{}``. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the @@ -323,6 +332,12 @@ affect the :func:`format` function. The meaning of the various alignment options is as follows: + .. index:: + single: <; in string formatting + single: >; in string formatting + single: =; in string formatting + single: ^; in string formatting + +---------+----------------------------------------------------------+ | Option | Meaning | +=========+==========================================================+ @@ -349,6 +364,11 @@ meaning in this case. The *sign* option is only valid for number types, and can be one of the following: + .. index:: + single: +; in string formatting + single: -; in string formatting + single: space; in string formatting + +---------+----------------------------------------------------------+ | Option | Meaning | +=========+==========================================================+ @@ -363,6 +383,8 @@ following: +---------+----------------------------------------------------------+ +.. index:: single: #; in string formatting + The ``'#'`` option causes the "alternate form" to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and @@ -375,6 +397,8 @@ decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for ``'g'`` and ``'G'`` conversions, trailing zeros are not removed from the result. +.. index:: single: ,; in string formatting + The ``','`` option signals the use of a comma for a thousands separator. For a locale aware separator, use the ``'n'`` integer presentation type instead. @@ -382,6 +406,8 @@ instead. .. versionchanged:: 3.1 Added the ``','`` option (see also :pep:`378`). +.. index:: single: _; in string formatting + The ``'_'`` option signals the use of an underscore for a thousands separator for floating point presentation types and for integer presentation type ``'d'``. For integer presentation types ``'b'``, @@ -660,9 +686,17 @@ Nesting arguments and more complex examples:: Template strings ---------------- -Templates provide simpler string substitutions as described in :pep:`292`. -Instead of the normal ``%``\ -based substitutions, Templates support ``$``\ --based substitutions, using the following rules: +Template strings provide simpler string substitutions as described in +:pep:`292`. A primary use case for template strings is for +internationalization (i18n) since in that context, the simpler syntax and +functionality makes it easier to translate than other built-in string +formatting facilities in Python. As an example of a library built on template +strings for i18n, see the +`flufl.i18n <http://flufli18n.readthedocs.io/en/latest/>`_ package. + +.. index:: single: $; in template strings + +Template strings support ``$``-based substitutions, using the following rules: * ``$$`` is an escape; it is replaced with a single ``$``. |