From eca60a7d81798b403a1c3b34f8c1dc59b9985622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Fri, 27 Sep 2019 20:30:11 -0300 Subject: [PATCH 1/2] Clarify indexes in format --- Doc/library/string.rst | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index e2983db1ac8321..3739f622070d3a 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -229,12 +229,13 @@ keyword. If it's a number, it refers to a positional argument, and if it's a ke it refers to a named keyword argument. If the numerical arg_names in a format string are 0, 1, 2, ... in sequence, they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be automatically inserted in that order. -Because *arg_name* is not quote-delimited, it is not possible to specify arbitrary -dictionary keys (e.g., the strings ``'10'`` or ``':-]'``) within a format string. The *arg_name* can be followed by any number of index or attribute expressions. An expression of the form ``'.name'`` selects the named attribute using :func:`getattr`, while an expression of the form ``'[index]'`` -does an index lookup using :func:`__getitem__`. +does an index lookup using :func:`__getitem__`. If *element_index* matches +*index_string*, then a string with value *element_index* will be used as key. +As *element_index* cannot be quote-delimited, it is not possible to use +a string key that matches `digit`+ such as ``'10'`` within a format string. .. versionchanged:: 3.1 The positional argument specifiers can be omitted for :meth:`str.format`, @@ -251,6 +252,7 @@ Some simple format string examples:: "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'. + "Welcome {player[name]}" # Value of key 'name' of keyword argument 'player' The *conversion* field causes a type coercion before formatting. Normally, the job of formatting a value is done by the :meth:`__format__` method of the value From f8c90d0fd544de127e9ed96f504e0278da3fcf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Delfino?= Date: Fri, 27 Sep 2019 20:44:00 -0300 Subject: [PATCH 2/2] Fix markup --- Doc/library/string.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 3739f622070d3a..4f5f961dbcca0e 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -235,7 +235,7 @@ attribute using :func:`getattr`, while an expression of the form ``'[index]'`` does an index lookup using :func:`__getitem__`. If *element_index* matches *index_string*, then a string with value *element_index* will be used as key. As *element_index* cannot be quote-delimited, it is not possible to use -a string key that matches `digit`+ such as ``'10'`` within a format string. +a string key that matches *digit+* such as ``'10'`` within a format string. .. versionchanged:: 3.1 The positional argument specifiers can be omitted for :meth:`str.format`,