| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`string` --- Common string operations | 
 | 2 | ========================================== | 
 | 3 |  | 
 | 4 | .. module:: string | 
 | 5 |    :synopsis: Common string operations. | 
 | 6 |  | 
 | 7 |  | 
| Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 8 | .. seealso:: | 
 | 9 |  | 
| Georg Brandl | b30f330 | 2011-01-06 09:23:56 +0000 | [diff] [blame] | 10 |    :ref:`typesseq` | 
 | 11 |  | 
 | 12 |    :ref:`string-methods` | 
 | 13 |  | 
| Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 14 | **Source code:** :source:`Lib/string.py` | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 |  | 
| Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 16 | -------------- | 
 | 17 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | String constants | 
 | 19 | ---------------- | 
 | 20 |  | 
 | 21 | The constants defined in this module are: | 
 | 22 |  | 
 | 23 |  | 
 | 24 | .. data:: ascii_letters | 
 | 25 |  | 
 | 26 |    The concatenation of the :const:`ascii_lowercase` and :const:`ascii_uppercase` | 
 | 27 |    constants described below.  This value is not locale-dependent. | 
 | 28 |  | 
 | 29 |  | 
 | 30 | .. data:: ascii_lowercase | 
 | 31 |  | 
 | 32 |    The lowercase letters ``'abcdefghijklmnopqrstuvwxyz'``.  This value is not | 
 | 33 |    locale-dependent and will not change. | 
 | 34 |  | 
 | 35 |  | 
 | 36 | .. data:: ascii_uppercase | 
 | 37 |  | 
 | 38 |    The uppercase letters ``'ABCDEFGHIJKLMNOPQRSTUVWXYZ'``.  This value is not | 
 | 39 |    locale-dependent and will not change. | 
 | 40 |  | 
 | 41 |  | 
 | 42 | .. data:: digits | 
 | 43 |  | 
 | 44 |    The string ``'0123456789'``. | 
 | 45 |  | 
 | 46 |  | 
 | 47 | .. data:: hexdigits | 
 | 48 |  | 
 | 49 |    The string ``'0123456789abcdefABCDEF'``. | 
 | 50 |  | 
 | 51 |  | 
 | 52 | .. data:: octdigits | 
 | 53 |  | 
 | 54 |    The string ``'01234567'``. | 
 | 55 |  | 
 | 56 |  | 
 | 57 | .. data:: punctuation | 
 | 58 |  | 
 | 59 |    String of ASCII characters which are considered punctuation characters | 
 | 60 |    in the ``C`` locale. | 
 | 61 |  | 
 | 62 |  | 
 | 63 | .. data:: printable | 
 | 64 |  | 
 | 65 |    String of ASCII characters which are considered printable.  This is a | 
 | 66 |    combination of :const:`digits`, :const:`ascii_letters`, :const:`punctuation`, | 
 | 67 |    and :const:`whitespace`. | 
 | 68 |  | 
 | 69 |  | 
 | 70 | .. data:: whitespace | 
 | 71 |  | 
| Georg Brandl | 5076740 | 2008-11-22 08:31:09 +0000 | [diff] [blame] | 72 |    A string containing all ASCII characters that are considered whitespace. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 |    This includes the characters space, tab, linefeed, return, formfeed, and | 
 | 74 |    vertical tab. | 
 | 75 |  | 
 | 76 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 77 | .. _string-formatting: | 
 | 78 |  | 
 | 79 | String Formatting | 
 | 80 | ----------------- | 
 | 81 |  | 
| Benjamin Peterson | 50923f9 | 2008-05-25 19:45:17 +0000 | [diff] [blame] | 82 | The built-in string class provides the ability to do complex variable | 
 | 83 | substitutions and value formatting via the :func:`format` method described in | 
 | 84 | :pep:`3101`.  The :class:`Formatter` class in the :mod:`string` module allows | 
 | 85 | you to create and customize your own string formatting behaviors using the same | 
 | 86 | implementation as the built-in :meth:`format` method. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 87 |  | 
| Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 88 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 89 | .. class:: Formatter | 
 | 90 |  | 
 | 91 |    The :class:`Formatter` class has the following public methods: | 
 | 92 |  | 
| Georg Brandl | 8e490de | 2011-01-24 19:53:18 +0000 | [diff] [blame] | 93 |    .. method:: format(format_string, *args, **kwargs) | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 94 |  | 
 | 95 |       :meth:`format` is the primary API method.  It takes a format template | 
 | 96 |       string, and an arbitrary set of positional and keyword argument. | 
 | 97 |       :meth:`format` is just a wrapper that calls :meth:`vformat`. | 
 | 98 |  | 
 | 99 |    .. method:: vformat(format_string, args, kwargs) | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 100 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 101 |       This function does the actual work of formatting.  It is exposed as a | 
 | 102 |       separate function for cases where you want to pass in a predefined | 
 | 103 |       dictionary of arguments, rather than unpacking and repacking the | 
 | 104 |       dictionary as individual arguments using the ``*args`` and ``**kwds`` | 
 | 105 |       syntax.  :meth:`vformat` does the work of breaking up the format template | 
 | 106 |       string into character data and replacement fields.  It calls the various | 
 | 107 |       methods described below. | 
 | 108 |  | 
 | 109 |    In addition, the :class:`Formatter` defines a number of methods that are | 
 | 110 |    intended to be replaced by subclasses: | 
 | 111 |  | 
 | 112 |    .. method:: parse(format_string) | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 113 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 114 |       Loop over the format_string and return an iterable of tuples | 
 | 115 |       (*literal_text*, *field_name*, *format_spec*, *conversion*).  This is used | 
| Georg Brandl | 70cd7bc | 2010-10-26 19:31:06 +0000 | [diff] [blame] | 116 |       by :meth:`vformat` to break the string into either literal text, or | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 117 |       replacement fields. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 118 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 119 |       The values in the tuple conceptually represent a span of literal text | 
 | 120 |       followed by a single replacement field.  If there is no literal text | 
 | 121 |       (which can happen if two replacement fields occur consecutively), then | 
 | 122 |       *literal_text* will be a zero-length string.  If there is no replacement | 
 | 123 |       field, then the values of *field_name*, *format_spec* and *conversion* | 
 | 124 |       will be ``None``. | 
 | 125 |  | 
| Eric Smith | 9d4ba39 | 2007-09-02 15:33:26 +0000 | [diff] [blame] | 126 |    .. method:: get_field(field_name, args, kwargs) | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 127 |  | 
 | 128 |       Given *field_name* as returned by :meth:`parse` (see above), convert it to | 
| Georg Brandl | 7f13e6b | 2007-08-31 10:37:15 +0000 | [diff] [blame] | 129 |       an object to be formatted.  Returns a tuple (obj, used_key).  The default | 
 | 130 |       version takes strings of the form defined in :pep:`3101`, such as | 
 | 131 |       "0[name]" or "label.title".  *args* and *kwargs* are as passed in to | 
 | 132 |       :meth:`vformat`.  The return value *used_key* has the same meaning as the | 
 | 133 |       *key* parameter to :meth:`get_value`. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 134 |  | 
 | 135 |    .. method:: get_value(key, args, kwargs) | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 136 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 137 |       Retrieve a given field value.  The *key* argument will be either an | 
 | 138 |       integer or a string.  If it is an integer, it represents the index of the | 
 | 139 |       positional argument in *args*; if it is a string, then it represents a | 
 | 140 |       named argument in *kwargs*. | 
 | 141 |  | 
 | 142 |       The *args* parameter is set to the list of positional arguments to | 
 | 143 |       :meth:`vformat`, and the *kwargs* parameter is set to the dictionary of | 
 | 144 |       keyword arguments. | 
 | 145 |  | 
 | 146 |       For compound field names, these functions are only called for the first | 
 | 147 |       component of the field name; Subsequent components are handled through | 
 | 148 |       normal attribute and indexing operations. | 
 | 149 |  | 
 | 150 |       So for example, the field expression '0.name' would cause | 
 | 151 |       :meth:`get_value` to be called with a *key* argument of 0.  The ``name`` | 
 | 152 |       attribute will be looked up after :meth:`get_value` returns by calling the | 
 | 153 |       built-in :func:`getattr` function. | 
 | 154 |  | 
 | 155 |       If the index or keyword refers to an item that does not exist, then an | 
 | 156 |       :exc:`IndexError` or :exc:`KeyError` should be raised. | 
 | 157 |  | 
 | 158 |    .. method:: check_unused_args(used_args, args, kwargs) | 
 | 159 |  | 
 | 160 |       Implement checking for unused arguments if desired.  The arguments to this | 
 | 161 |       function is the set of all argument keys that were actually referred to in | 
 | 162 |       the format string (integers for positional arguments, and strings for | 
 | 163 |       named arguments), and a reference to the *args* and *kwargs* that was | 
 | 164 |       passed to vformat.  The set of unused args can be calculated from these | 
| Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 165 |       parameters.  :meth:`check_unused_args` is assumed to raise an exception if | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 166 |       the check fails. | 
 | 167 |  | 
 | 168 |    .. method:: format_field(value, format_spec) | 
 | 169 |  | 
 | 170 |       :meth:`format_field` simply calls the global :func:`format` built-in.  The | 
 | 171 |       method is provided so that subclasses can override it. | 
 | 172 |  | 
 | 173 |    .. method:: convert_field(value, conversion) | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 174 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 175 |       Converts the value (returned by :meth:`get_field`) given a conversion type | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 176 |       (as in the tuple returned by the :meth:`parse` method).  The default | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 177 |       version understands 'r' (repr) and 's' (str) conversion types. | 
 | 178 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 179 |  | 
 | 180 | .. _formatstrings: | 
 | 181 |  | 
 | 182 | Format String Syntax | 
 | 183 | -------------------- | 
 | 184 |  | 
 | 185 | The :meth:`str.format` method and the :class:`Formatter` class share the same | 
 | 186 | syntax for format strings (although in the case of :class:`Formatter`, | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 187 | subclasses can define their own format string syntax). | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 188 |  | 
 | 189 | Format strings contain "replacement fields" surrounded by curly braces ``{}``. | 
 | 190 | Anything that is not contained in braces is considered literal text, which is | 
 | 191 | copied unchanged to the output.  If you need to include a brace character in the | 
 | 192 | literal text, it can be escaped by doubling: ``{{`` and ``}}``. | 
 | 193 |  | 
 | 194 | The grammar for a replacement field is as follows: | 
 | 195 |  | 
 | 196 |    .. productionlist:: sf | 
| Georg Brandl | 2f3ed68 | 2009-09-01 07:42:40 +0000 | [diff] [blame] | 197 |       replacement_field: "{" [`field_name`] ["!" `conversion`] [":" `format_spec`] "}" | 
| Eric Smith | c4cae32 | 2009-04-22 00:53:01 +0000 | [diff] [blame] | 198 |       field_name: arg_name ("." `attribute_name` | "[" `element_index` "]")* | 
| Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 199 |       arg_name: [`identifier` | `integer`] | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 200 |       attribute_name: `identifier` | 
| Eric Smith | 2e9f202 | 2010-02-25 14:58:13 +0000 | [diff] [blame] | 201 |       element_index: `integer` | `index_string` | 
 | 202 |       index_string: <any source character except "]"> + | 
| Benjamin Peterson | 065ba70 | 2008-11-09 01:43:02 +0000 | [diff] [blame] | 203 |       conversion: "r" | "s" | "a" | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 204 |       format_spec: <described in the next section> | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 205 |  | 
| Georg Brandl | 2f3ed68 | 2009-09-01 07:42:40 +0000 | [diff] [blame] | 206 | In less formal terms, the replacement field can start with a *field_name* that specifies | 
| Eric Smith | c4cae32 | 2009-04-22 00:53:01 +0000 | [diff] [blame] | 207 | the object whose value is to be formatted and inserted | 
 | 208 | into the output instead of the replacement field. | 
 | 209 | The *field_name* is optionally followed by a  *conversion* field, which is | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 210 | preceded by an exclamation point ``'!'``, and a *format_spec*, which is preceded | 
| Eric Smith | c4cae32 | 2009-04-22 00:53:01 +0000 | [diff] [blame] | 211 | by a colon ``':'``.  These specify a non-default format for the replacement value. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 212 |  | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 213 | See also the :ref:`formatspec` section. | 
 | 214 |  | 
| Eric Smith | c4cae32 | 2009-04-22 00:53:01 +0000 | [diff] [blame] | 215 | The *field_name* itself begins with an *arg_name* that is either either a number or a | 
 | 216 | keyword.  If it's a number, it refers to a positional argument, and if it's a keyword, | 
 | 217 | it refers to a named keyword argument.  If the numerical arg_names in a format string | 
 | 218 | are 0, 1, 2, ... in sequence, they can all be omitted (not just some) | 
 | 219 | and the numbers 0, 1, 2, ... will be automatically inserted in that order. | 
 | 220 | The *arg_name* can be followed by any number of index or | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 221 | attribute expressions. An expression of the form ``'.name'`` selects the named | 
 | 222 | attribute using :func:`getattr`, while an expression of the form ``'[index]'`` | 
 | 223 | does an index lookup using :func:`__getitem__`. | 
 | 224 |  | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 225 | .. versionchanged:: 3.1 | 
 | 226 |    The positional argument specifiers can be omitted, so ``'{} {}'`` is | 
 | 227 |    equivalent to ``'{0} {1}'``. | 
 | 228 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 229 | Some simple format string examples:: | 
 | 230 |  | 
 | 231 |    "First, thou shalt count to {0}" # References first positional argument | 
| Benjamin Peterson | 5879d41 | 2009-03-30 14:51:56 +0000 | [diff] [blame] | 232 |    "Bring me a {}"                  # Implicitly references the first positional argument | 
| Georg Brandl | 2f3ed68 | 2009-09-01 07:42:40 +0000 | [diff] [blame] | 233 |    "From {} to {}"                  # Same as "From {0} to {1}" | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 234 |    "My quest is {name}"             # References keyword argument 'name' | 
 | 235 |    "Weight in tons {0.weight}"      # 'weight' attribute of first positional arg | 
 | 236 |    "Units destroyed: {players[0]}"  # First element of keyword argument 'players'. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 237 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 238 | The *conversion* field causes a type coercion before formatting.  Normally, the | 
 | 239 | job of formatting a value is done by the :meth:`__format__` method of the value | 
 | 240 | itself.  However, in some cases it is desirable to force a type to be formatted | 
 | 241 | as a string, overriding its own definition of formatting.  By converting the | 
 | 242 | value to a string before calling :meth:`__format__`, the normal formatting logic | 
 | 243 | is bypassed. | 
 | 244 |  | 
| Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 245 | Three conversion flags are currently supported: ``'!s'`` which calls :func:`str` | 
 | 246 | on the value, ``'!r'`` which calls :func:`repr` and ``'!a'`` which calls | 
 | 247 | :func:`ascii`. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 248 |  | 
 | 249 | Some examples:: | 
 | 250 |  | 
 | 251 |    "Harold's a clever {0!s}"        # Calls str() on the argument first | 
 | 252 |    "Bring out the holy {name!r}"    # Calls repr() on the argument first | 
| Georg Brandl | 2f3ed68 | 2009-09-01 07:42:40 +0000 | [diff] [blame] | 253 |    "More {!a}"                      # Calls ascii() on the argument first | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 254 |  | 
 | 255 | The *format_spec* field contains a specification of how the value should be | 
 | 256 | presented, including such details as field width, alignment, padding, decimal | 
| Eric Smith | 0f7affe | 2010-02-15 11:57:31 +0000 | [diff] [blame] | 257 | precision and so on.  Each value type can define its own "formatting | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 258 | mini-language" or interpretation of the *format_spec*. | 
 | 259 |  | 
 | 260 | Most built-in types support a common formatting mini-language, which is | 
 | 261 | described in the next section. | 
 | 262 |  | 
 | 263 | A *format_spec* field can also include nested replacement fields within it. | 
 | 264 | These nested replacement fields can contain only a field name; conversion flags | 
 | 265 | and format specifications are not allowed.  The replacement fields within the | 
 | 266 | format_spec are substituted before the *format_spec* string is interpreted. | 
 | 267 | This allows the formatting of a value to be dynamically specified. | 
 | 268 |  | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 269 | See the :ref:`formatexamples` section for some examples. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 270 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 271 |  | 
 | 272 | .. _formatspec: | 
 | 273 |  | 
 | 274 | Format Specification Mini-Language | 
 | 275 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 
 | 276 |  | 
 | 277 | "Format specifications" are used within replacement fields contained within a | 
 | 278 | format string to define how individual values are presented (see | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 279 | :ref:`formatstrings`).  They can also be passed directly to the built-in | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 280 | :func:`format` function.  Each formattable type may define how the format | 
 | 281 | specification is to be interpreted. | 
 | 282 |  | 
 | 283 | Most built-in types implement the following options for format specifications, | 
 | 284 | although some of the formatting options are only supported by the numeric types. | 
 | 285 |  | 
| Eric Smith | 05c0774 | 2010-02-25 14:18:57 +0000 | [diff] [blame] | 286 | A general convention is that an empty format string (``""``) produces | 
 | 287 | the same result as if you had called :func:`str` on the value. A | 
 | 288 | non-empty format string typically modifies the result. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 289 |  | 
 | 290 | The general form of a *standard format specifier* is: | 
 | 291 |  | 
 | 292 | .. productionlist:: sf | 
| Raymond Hettinger | 6db9470 | 2009-07-12 20:49:21 +0000 | [diff] [blame] | 293 |    format_spec: [[`fill`]`align`][`sign`][#][0][`width`][,][.`precision`][`type`] | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 294 |    fill: <a character other than '}'> | 
 | 295 |    align: "<" | ">" | "=" | "^" | 
 | 296 |    sign: "+" | "-" | " " | 
 | 297 |    width: `integer` | 
 | 298 |    precision: `integer` | 
| Eric Smith | 05c0774 | 2010-02-25 14:18:57 +0000 | [diff] [blame] | 299 |    type: "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 300 |  | 
| Georg Brandl | c86adb4 | 2010-09-06 06:49:07 +0000 | [diff] [blame] | 301 | The *fill* character can be any character other than '{' or '}'.  The presence | 
 | 302 | of a fill character is signaled by the character following it, which must be | 
 | 303 | one of the alignment options.  If the second character of *format_spec* is not | 
 | 304 | a valid alignment option, then it is assumed that both the fill character and | 
 | 305 | the alignment option are absent. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 306 |  | 
 | 307 | The meaning of the various alignment options is as follows: | 
 | 308 |  | 
 | 309 |    +---------+----------------------------------------------------------+ | 
 | 310 |    | Option  | Meaning                                                  | | 
 | 311 |    +=========+==========================================================+ | 
 | 312 |    | ``'<'`` | Forces the field to be left-aligned within the available | | 
| Georg Brandl | ca583b6 | 2011-02-07 12:13:58 +0000 | [diff] [blame] | 313 |    |         | space (this is the default for most objects).            | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 314 |    +---------+----------------------------------------------------------+ | 
 | 315 |    | ``'>'`` | Forces the field to be right-aligned within the          | | 
| Georg Brandl | ca583b6 | 2011-02-07 12:13:58 +0000 | [diff] [blame] | 316 |    |         | available space (this is the default for numbers).       | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 317 |    +---------+----------------------------------------------------------+ | 
 | 318 |    | ``'='`` | Forces the padding to be placed after the sign (if any)  | | 
 | 319 |    |         | but before the digits.  This is used for printing fields | | 
 | 320 |    |         | in the form '+000000120'. This alignment option is only  | | 
 | 321 |    |         | valid for numeric types.                                 | | 
 | 322 |    +---------+----------------------------------------------------------+ | 
 | 323 |    | ``'^'`` | Forces the field to be centered within the available     | | 
 | 324 |    |         | space.                                                   | | 
 | 325 |    +---------+----------------------------------------------------------+ | 
 | 326 |  | 
 | 327 | Note that unless a minimum field width is defined, the field width will always | 
 | 328 | be the same size as the data to fill it, so that the alignment option has no | 
 | 329 | meaning in this case. | 
 | 330 |  | 
 | 331 | The *sign* option is only valid for number types, and can be one of the | 
 | 332 | following: | 
 | 333 |  | 
 | 334 |    +---------+----------------------------------------------------------+ | 
 | 335 |    | Option  | Meaning                                                  | | 
 | 336 |    +=========+==========================================================+ | 
 | 337 |    | ``'+'`` | indicates that a sign should be used for both            | | 
 | 338 |    |         | positive as well as negative numbers.                    | | 
 | 339 |    +---------+----------------------------------------------------------+ | 
 | 340 |    | ``'-'`` | indicates that a sign should be used only for negative   | | 
 | 341 |    |         | numbers (this is the default behavior).                  | | 
 | 342 |    +---------+----------------------------------------------------------+ | 
 | 343 |    | space   | indicates that a leading space should be used on         | | 
 | 344 |    |         | positive numbers, and a minus sign on negative numbers.  | | 
 | 345 |    +---------+----------------------------------------------------------+ | 
 | 346 |  | 
| Eric Smith | 984bb58 | 2010-11-25 16:08:06 +0000 | [diff] [blame] | 347 |  | 
 | 348 | The ``'#'`` option causes the "alternate form" to be used for the | 
 | 349 | conversion.  The alternate form is defined differently for different | 
 | 350 | types.  This option is only valid for integer, float, complex and | 
 | 351 | Decimal types. For integers, when binary, octal, or hexadecimal output | 
 | 352 | is used, this option adds the prefix respective ``'0b'``, ``'0o'``, or | 
 | 353 | ``'0x'`` to the output value. For floats, complex and Decimal the | 
 | 354 | alternate form causes the result of the conversion to always contain a | 
 | 355 | decimal-point character, even if no digits follow it. Normally, a | 
 | 356 | decimal-point character appears in the result of these conversions | 
 | 357 | only if a digit follows it. In addition, for ``'g'`` and ``'G'`` | 
 | 358 | conversions, trailing zeros are not removed from the result. | 
| Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 359 |  | 
| Raymond Hettinger | 6db9470 | 2009-07-12 20:49:21 +0000 | [diff] [blame] | 360 | The ``','`` option signals the use of a comma for a thousands separator. | 
 | 361 | For a locale aware separator, use the ``'n'`` integer presentation type | 
 | 362 | instead. | 
 | 363 |  | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 364 | .. versionchanged:: 3.1 | 
 | 365 |    Added the ``','`` option (see also :pep:`378`). | 
 | 366 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 367 | *width* is a decimal integer defining the minimum field width.  If not | 
 | 368 | specified, then the field width will be determined by the content. | 
 | 369 |  | 
 | 370 | If the *width* field is preceded by a zero (``'0'``) character, this enables | 
 | 371 | zero-padding.  This is equivalent to an *alignment* type of ``'='`` and a *fill* | 
 | 372 | character of ``'0'``. | 
 | 373 |  | 
 | 374 | The *precision* is a decimal number indicating how many digits should be | 
| Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 375 | displayed after the decimal point for a floating point value formatted with | 
 | 376 | ``'f'`` and ``'F'``, or before and after the decimal point for a floating point | 
 | 377 | value formatted with ``'g'`` or ``'G'``.  For non-number types the field | 
 | 378 | indicates the maximum field size - in other words, how many characters will be | 
| Eric Smith | e5fffc7 | 2009-05-07 19:38:09 +0000 | [diff] [blame] | 379 | used from the field content. The *precision* is not allowed for integer values. | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 380 |  | 
 | 381 | Finally, the *type* determines how the data should be presented. | 
 | 382 |  | 
| Eric Smith | 05c0774 | 2010-02-25 14:18:57 +0000 | [diff] [blame] | 383 | The available string presentation types are: | 
 | 384 |  | 
 | 385 |    +---------+----------------------------------------------------------+ | 
 | 386 |    | Type    | Meaning                                                  | | 
 | 387 |    +=========+==========================================================+ | 
 | 388 |    | ``'s'`` | String format. This is the default type for strings and  | | 
 | 389 |    |         | may be omitted.                                          | | 
 | 390 |    +---------+----------------------------------------------------------+ | 
 | 391 |    | None    | The same as ``'s'``.                                     | | 
 | 392 |    +---------+----------------------------------------------------------+ | 
 | 393 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 394 | The available integer presentation types are: | 
 | 395 |  | 
 | 396 |    +---------+----------------------------------------------------------+ | 
 | 397 |    | Type    | Meaning                                                  | | 
 | 398 |    +=========+==========================================================+ | 
| Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 399 |    | ``'b'`` | Binary format. Outputs the number in base 2.             | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 400 |    +---------+----------------------------------------------------------+ | 
 | 401 |    | ``'c'`` | Character. Converts the integer to the corresponding     | | 
 | 402 |    |         | unicode character before printing.                       | | 
 | 403 |    +---------+----------------------------------------------------------+ | 
 | 404 |    | ``'d'`` | Decimal Integer. Outputs the number in base 10.          | | 
 | 405 |    +---------+----------------------------------------------------------+ | 
 | 406 |    | ``'o'`` | Octal format. Outputs the number in base 8.              | | 
 | 407 |    +---------+----------------------------------------------------------+ | 
 | 408 |    | ``'x'`` | Hex format. Outputs the number in base 16, using lower-  | | 
 | 409 |    |         | case letters for the digits above 9.                     | | 
 | 410 |    +---------+----------------------------------------------------------+ | 
 | 411 |    | ``'X'`` | Hex format. Outputs the number in base 16, using upper-  | | 
 | 412 |    |         | case letters for the digits above 9.                     | | 
 | 413 |    +---------+----------------------------------------------------------+ | 
| Eric Smith | 5e18a20 | 2008-05-12 10:01:24 +0000 | [diff] [blame] | 414 |    | ``'n'`` | Number. This is the same as ``'d'``, except that it uses | | 
 | 415 |    |         | the current locale setting to insert the appropriate     | | 
 | 416 |    |         | number separator characters.                             | | 
 | 417 |    +---------+----------------------------------------------------------+ | 
| Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 418 |    | None    | The same as ``'d'``.                                     | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 419 |    +---------+----------------------------------------------------------+ | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 420 |  | 
| Eric Smith | 05c0774 | 2010-02-25 14:18:57 +0000 | [diff] [blame] | 421 | In addition to the above presentation types, integers can be formatted | 
 | 422 | with the floating point presentation types listed below (except | 
 | 423 | ``'n'`` and None). When doing so, :func:`float` is used to convert the | 
 | 424 | integer to a floating point number before formatting. | 
 | 425 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 426 | The available presentation types for floating point and decimal values are: | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 427 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 428 |    +---------+----------------------------------------------------------+ | 
 | 429 |    | Type    | Meaning                                                  | | 
 | 430 |    +=========+==========================================================+ | 
 | 431 |    | ``'e'`` | Exponent notation. Prints the number in scientific       | | 
 | 432 |    |         | notation using the letter 'e' to indicate the exponent.  | | 
 | 433 |    +---------+----------------------------------------------------------+ | 
| Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 434 |    | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an     | | 
 | 435 |    |         | upper case 'E' as the separator character.               | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 436 |    +---------+----------------------------------------------------------+ | 
 | 437 |    | ``'f'`` | Fixed point. Displays the number as a fixed-point        | | 
 | 438 |    |         | number.                                                  | | 
 | 439 |    +---------+----------------------------------------------------------+ | 
| Eric Smith | 741191f | 2009-05-06 13:08:15 +0000 | [diff] [blame] | 440 |    | ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to    | | 
 | 441 |    |         | ``NAN`` and ``inf`` to ``INF``.                          | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 442 |    +---------+----------------------------------------------------------+ | 
| Mark Dickinson | c70614f | 2009-10-08 20:05:48 +0000 | [diff] [blame] | 443 |    | ``'g'`` | General format.  For a given precision ``p >= 1``,       | | 
 | 444 |    |         | this rounds the number to ``p`` significant digits and   | | 
 | 445 |    |         | then formats the result in either fixed-point format     | | 
 | 446 |    |         | or in scientific notation, depending on its magnitude.   | | 
 | 447 |    |         |                                                          | | 
 | 448 |    |         | The precise rules are as follows: suppose that the       | | 
 | 449 |    |         | result formatted with presentation type ``'e'`` and      | | 
 | 450 |    |         | precision ``p-1`` would have exponent ``exp``.  Then     | | 
 | 451 |    |         | if ``-4 <= exp < p``, the number is formatted            | | 
 | 452 |    |         | with presentation type ``'f'`` and precision             | | 
 | 453 |    |         | ``p-1-exp``.  Otherwise, the number is formatted         | | 
 | 454 |    |         | with presentation type ``'e'`` and precision ``p-1``.    | | 
 | 455 |    |         | In both cases insignificant trailing zeros are removed   | | 
 | 456 |    |         | from the significand, and the decimal point is also      | | 
 | 457 |    |         | removed if there are no remaining digits following it.   | | 
 | 458 |    |         |                                                          | | 
| Benjamin Peterson | 73a3f2d | 2010-10-12 23:07:13 +0000 | [diff] [blame] | 459 |    |         | Positive and negative infinity, positive and negative    | | 
| Mark Dickinson | c70614f | 2009-10-08 20:05:48 +0000 | [diff] [blame] | 460 |    |         | zero, and nans, are formatted as ``inf``, ``-inf``,      | | 
 | 461 |    |         | ``0``, ``-0`` and ``nan`` respectively, regardless of    | | 
 | 462 |    |         | the precision.                                           | | 
 | 463 |    |         |                                                          | | 
 | 464 |    |         | A precision of ``0`` is treated as equivalent to a       | | 
 | 465 |    |         | precision of ``1``.                                      | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 466 |    +---------+----------------------------------------------------------+ | 
 | 467 |    | ``'G'`` | General format. Same as ``'g'`` except switches to       | | 
| Mark Dickinson | c70614f | 2009-10-08 20:05:48 +0000 | [diff] [blame] | 468 |    |         | ``'E'`` if the number gets too large. The                | | 
 | 469 |    |         | representations of infinity and NaN are uppercased, too. | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 470 |    +---------+----------------------------------------------------------+ | 
 | 471 |    | ``'n'`` | Number. This is the same as ``'g'``, except that it uses | | 
 | 472 |    |         | the current locale setting to insert the appropriate     | | 
 | 473 |    |         | number separator characters.                             | | 
 | 474 |    +---------+----------------------------------------------------------+ | 
 | 475 |    | ``'%'`` | Percentage. Multiplies the number by 100 and displays    | | 
 | 476 |    |         | in fixed (``'f'``) format, followed by a percent sign.   | | 
 | 477 |    +---------+----------------------------------------------------------+ | 
| Eric Smith | 3bef15b | 2009-05-05 17:19:46 +0000 | [diff] [blame] | 478 |    | None    | Similar to ``'g'``, except with at least one digit past  | | 
 | 479 |    |         | the decimal point and a default precision of 12. This is | | 
 | 480 |    |         | intended to match :func:`str`, except you can add the    | | 
 | 481 |    |         | other format modifiers.                                  | | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 482 |    +---------+----------------------------------------------------------+ | 
 | 483 |  | 
 | 484 |  | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 485 | .. _formatexamples: | 
 | 486 |  | 
 | 487 | Format examples | 
 | 488 | ^^^^^^^^^^^^^^^ | 
 | 489 |  | 
 | 490 | This section contains examples of the new format syntax and comparison with | 
 | 491 | the old ``%``-formatting. | 
 | 492 |  | 
 | 493 | In most of the cases the syntax is similar to the old ``%``-formatting, with the | 
 | 494 | addition of the ``{}`` and with ``:`` used instead of ``%``. | 
 | 495 | For example, ``'%03.2f'`` can be translated to ``'{:03.2f}'``. | 
 | 496 |  | 
 | 497 | The new format syntax also supports new and different options, shown in the | 
 | 498 | follow examples. | 
 | 499 |  | 
 | 500 | Accessing arguments by position:: | 
 | 501 |  | 
 | 502 |    >>> '{0}, {1}, {2}'.format('a', 'b', 'c') | 
 | 503 |    'a, b, c' | 
 | 504 |    >>> '{}, {}, {}'.format('a', 'b', 'c')  # 3.1+ only | 
 | 505 |    'a, b, c' | 
 | 506 |    >>> '{2}, {1}, {0}'.format('a', 'b', 'c') | 
 | 507 |    'c, b, a' | 
 | 508 |    >>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence | 
 | 509 |    'c, b, a' | 
 | 510 |    >>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated | 
 | 511 |    'abracadabra' | 
 | 512 |  | 
 | 513 | Accessing arguments by name:: | 
 | 514 |  | 
 | 515 |    >>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W') | 
 | 516 |    'Coordinates: 37.24N, -115.81W' | 
 | 517 |    >>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'} | 
 | 518 |    >>> 'Coordinates: {latitude}, {longitude}'.format(**coord) | 
 | 519 |    'Coordinates: 37.24N, -115.81W' | 
 | 520 |  | 
 | 521 | Accessing arguments' attributes:: | 
 | 522 |  | 
 | 523 |    >>> c = 3-5j | 
 | 524 |    >>> ('The complex number {0} is formed from the real part {0.real} ' | 
 | 525 |    ...  'and the imaginary part {0.imag}.').format(c) | 
 | 526 |    'The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.' | 
 | 527 |    >>> class Point: | 
 | 528 |    ...     def __init__(self, x, y): | 
 | 529 |    ...         self.x, self.y = x, y | 
 | 530 |    ...     def __str__(self): | 
 | 531 |    ...         return 'Point({self.x}, {self.y})'.format(self=self) | 
 | 532 |    ... | 
 | 533 |    >>> str(Point(4, 2)) | 
 | 534 |    'Point(4, 2)' | 
 | 535 |  | 
 | 536 | Accessing arguments' items:: | 
 | 537 |  | 
 | 538 |    >>> coord = (3, 5) | 
 | 539 |    >>> 'X: {0[0]};  Y: {0[1]}'.format(coord) | 
 | 540 |    'X: 3;  Y: 5' | 
 | 541 |  | 
 | 542 | Replacing ``%s`` and ``%r``:: | 
 | 543 |  | 
 | 544 |    >>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2') | 
 | 545 |    "repr() shows quotes: 'test1'; str() doesn't: test2" | 
 | 546 |  | 
 | 547 | Aligning the text and specifying a width:: | 
 | 548 |  | 
 | 549 |    >>> '{:<30}'.format('left aligned') | 
 | 550 |    'left aligned                  ' | 
 | 551 |    >>> '{:>30}'.format('right aligned') | 
 | 552 |    '                 right aligned' | 
 | 553 |    >>> '{:^30}'.format('centered') | 
 | 554 |    '           centered           ' | 
 | 555 |    >>> '{:*^30}'.format('centered')  # use '*' as a fill char | 
 | 556 |    '***********centered***********' | 
 | 557 |  | 
 | 558 | Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign:: | 
 | 559 |  | 
 | 560 |    >>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always | 
 | 561 |    '+3.140000; -3.140000' | 
 | 562 |    >>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers | 
 | 563 |    ' 3.140000; -3.140000' | 
 | 564 |    >>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}' | 
 | 565 |    '3.140000; -3.140000' | 
 | 566 |  | 
 | 567 | Replacing ``%x`` and ``%o`` and converting the value to different bases:: | 
 | 568 |  | 
 | 569 |    >>> # format also supports binary numbers | 
 | 570 |    >>> "int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42) | 
 | 571 |    'int: 42;  hex: 2a;  oct: 52;  bin: 101010' | 
 | 572 |    >>> # with 0x, 0o, or 0b as prefix: | 
 | 573 |    >>> "int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42) | 
 | 574 |    'int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010' | 
 | 575 |  | 
 | 576 | Using the comma as a thousands separator:: | 
 | 577 |  | 
 | 578 |    >>> '{:,}'.format(1234567890) | 
 | 579 |    '1,234,567,890' | 
 | 580 |  | 
 | 581 | Expressing a percentage:: | 
 | 582 |  | 
 | 583 |    >>> points = 19 | 
 | 584 |    >>> total = 22 | 
 | 585 |    >>> 'Correct answers: {:.2%}.'.format(points/total) | 
 | 586 |    'Correct answers: 86.36%' | 
 | 587 |  | 
 | 588 | Using type-specific formatting:: | 
 | 589 |  | 
 | 590 |    >>> import datetime | 
 | 591 |    >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58) | 
 | 592 |    >>> '{:%Y-%m-%d %H:%M:%S}'.format(d) | 
 | 593 |    '2010-07-04 12:15:58' | 
 | 594 |  | 
 | 595 | Nesting arguments and more complex examples:: | 
 | 596 |  | 
 | 597 |    >>> for align, text in zip('<^>', ['left', 'center', 'right']): | 
| Georg Brandl | a5770aa | 2011-02-07 12:10:46 +0000 | [diff] [blame] | 598 |    ...     '{0:{fill}{align}16}'.format(text, fill=align, align=align) | 
| Ezio Melotti | d2191e0 | 2010-07-02 23:18:51 +0000 | [diff] [blame] | 599 |    ... | 
 | 600 |    'left<<<<<<<<<<<<' | 
 | 601 |    '^^^^^center^^^^^' | 
 | 602 |    '>>>>>>>>>>>right' | 
 | 603 |    >>> | 
 | 604 |    >>> octets = [192, 168, 0, 1] | 
 | 605 |    >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets) | 
 | 606 |    'C0A80001' | 
 | 607 |    >>> int(_, 16) | 
 | 608 |    3232235521 | 
 | 609 |    >>> | 
 | 610 |    >>> width = 5 | 
 | 611 |    >>> for num in range(5,12): | 
 | 612 |    ...     for base in 'dXob': | 
 | 613 |    ...         print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ') | 
 | 614 |    ...     print() | 
 | 615 |    ... | 
 | 616 |        5     5     5   101 | 
 | 617 |        6     6     6   110 | 
 | 618 |        7     7     7   111 | 
 | 619 |        8     8    10  1000 | 
 | 620 |        9     9    11  1001 | 
 | 621 |       10     A    12  1010 | 
 | 622 |       11     B    13  1011 | 
 | 623 |  | 
 | 624 |  | 
 | 625 |  | 
| Georg Brandl | 4b49131 | 2007-08-31 09:22:56 +0000 | [diff] [blame] | 626 | .. _template-strings: | 
 | 627 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 628 | Template strings | 
 | 629 | ---------------- | 
 | 630 |  | 
 | 631 | Templates provide simpler string substitutions as described in :pep:`292`. | 
 | 632 | Instead of the normal ``%``\ -based substitutions, Templates support ``$``\ | 
 | 633 | -based substitutions, using the following rules: | 
 | 634 |  | 
 | 635 | * ``$$`` is an escape; it is replaced with a single ``$``. | 
 | 636 |  | 
 | 637 | * ``$identifier`` names a substitution placeholder matching a mapping key of | 
 | 638 |   ``"identifier"``.  By default, ``"identifier"`` must spell a Python | 
 | 639 |   identifier.  The first non-identifier character after the ``$`` character | 
 | 640 |   terminates this placeholder specification. | 
 | 641 |  | 
 | 642 | * ``${identifier}`` is equivalent to ``$identifier``.  It is required when valid | 
 | 643 |   identifier characters follow the placeholder but are not part of the | 
 | 644 |   placeholder, such as ``"${noun}ification"``. | 
 | 645 |  | 
 | 646 | Any other appearance of ``$`` in the string will result in a :exc:`ValueError` | 
 | 647 | being raised. | 
 | 648 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 649 | The :mod:`string` module provides a :class:`Template` class that implements | 
 | 650 | these rules.  The methods of :class:`Template` are: | 
 | 651 |  | 
 | 652 |  | 
 | 653 | .. class:: Template(template) | 
 | 654 |  | 
 | 655 |    The constructor takes a single argument which is the template string. | 
 | 656 |  | 
 | 657 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 658 |    .. method:: substitute(mapping, **kwds) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 659 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 660 |       Performs the template substitution, returning a new string.  *mapping* is | 
 | 661 |       any dictionary-like object with keys that match the placeholders in the | 
 | 662 |       template.  Alternatively, you can provide keyword arguments, where the | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 663 |       keywords are the placeholders.  When both *mapping* and *kwds* are given | 
 | 664 |       and there are duplicates, the placeholders from *kwds* take precedence. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 665 |  | 
 | 666 |  | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 667 |    .. method:: safe_substitute(mapping, **kwds) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 668 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 669 |       Like :meth:`substitute`, except that if placeholders are missing from | 
| Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 670 |       *mapping* and *kwds*, instead of raising a :exc:`KeyError` exception, the | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 671 |       original placeholder will appear in the resulting string intact.  Also, | 
 | 672 |       unlike with :meth:`substitute`, any other appearances of the ``$`` will | 
 | 673 |       simply return ``$`` instead of raising :exc:`ValueError`. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 674 |  | 
| Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 675 |       While other exceptions may still occur, this method is called "safe" | 
 | 676 |       because substitutions always tries to return a usable string instead of | 
 | 677 |       raising an exception.  In another sense, :meth:`safe_substitute` may be | 
 | 678 |       anything other than safe, since it will silently ignore malformed | 
 | 679 |       templates containing dangling delimiters, unmatched braces, or | 
 | 680 |       placeholders that are not valid Python identifiers. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 681 |  | 
| Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 682 |    :class:`Template` instances also provide one public data attribute: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 683 |  | 
| Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 684 |    .. attribute:: template | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 685 |  | 
| Benjamin Peterson | 2021100 | 2009-11-25 18:34:42 +0000 | [diff] [blame] | 686 |       This is the object passed to the constructor's *template* argument.  In | 
 | 687 |       general, you shouldn't change it, but read-only access is not enforced. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 688 |  | 
| Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 689 | Here is an example of how to use a Template: | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 690 |  | 
 | 691 |    >>> from string import Template | 
 | 692 |    >>> s = Template('$who likes $what') | 
 | 693 |    >>> s.substitute(who='tim', what='kung pao') | 
 | 694 |    'tim likes kung pao' | 
 | 695 |    >>> d = dict(who='tim') | 
 | 696 |    >>> Template('Give $who $100').substitute(d) | 
 | 697 |    Traceback (most recent call last): | 
 | 698 |    [...] | 
 | 699 |    ValueError: Invalid placeholder in string: line 1, col 10 | 
 | 700 |    >>> Template('$who likes $what').substitute(d) | 
 | 701 |    Traceback (most recent call last): | 
 | 702 |    [...] | 
 | 703 |    KeyError: 'what' | 
 | 704 |    >>> Template('$who likes $what').safe_substitute(d) | 
 | 705 |    'tim likes $what' | 
 | 706 |  | 
 | 707 | Advanced usage: you can derive subclasses of :class:`Template` to customize the | 
 | 708 | placeholder syntax, delimiter character, or the entire regular expression used | 
 | 709 | to parse template strings.  To do this, you can override these class attributes: | 
 | 710 |  | 
 | 711 | * *delimiter* -- This is the literal string describing a placeholder introducing | 
 | 712 |   delimiter.  The default value ``$``.  Note that this should *not* be a regular | 
 | 713 |   expression, as the implementation will call :meth:`re.escape` on this string as | 
 | 714 |   needed. | 
 | 715 |  | 
 | 716 | * *idpattern* -- This is the regular expression describing the pattern for | 
 | 717 |   non-braced placeholders (the braces will be added automatically as | 
 | 718 |   appropriate).  The default value is the regular expression | 
 | 719 |   ``[_a-z][_a-z0-9]*``. | 
 | 720 |  | 
| Georg Brandl | 056cb93 | 2010-07-29 17:16:10 +0000 | [diff] [blame] | 721 | * *flags* -- The regular expression flags that will be applied when compiling | 
 | 722 |   the regular expression used for recognizing substitutions.  The default value | 
 | 723 |   is ``re.IGNORECASE``.  Note that ``re.VERBOSE`` will always be added to the | 
 | 724 |   flags, so custom *idpattern*\ s must follow conventions for verbose regular | 
 | 725 |   expressions. | 
 | 726 |  | 
 | 727 |   .. versionadded:: 3.2 | 
 | 728 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 729 | Alternatively, you can provide the entire regular expression pattern by | 
 | 730 | overriding the class attribute *pattern*.  If you do this, the value must be a | 
 | 731 | regular expression object with four named capturing groups.  The capturing | 
 | 732 | groups correspond to the rules given above, along with the invalid placeholder | 
 | 733 | rule: | 
 | 734 |  | 
 | 735 | * *escaped* -- This group matches the escape sequence, e.g. ``$$``, in the | 
 | 736 |   default pattern. | 
 | 737 |  | 
 | 738 | * *named* -- This group matches the unbraced placeholder name; it should not | 
 | 739 |   include the delimiter in capturing group. | 
 | 740 |  | 
 | 741 | * *braced* -- This group matches the brace enclosed placeholder name; it should | 
 | 742 |   not include either the delimiter or braces in the capturing group. | 
 | 743 |  | 
 | 744 | * *invalid* -- This group matches any other delimiter pattern (usually a single | 
 | 745 |   delimiter), and it should appear last in the regular expression. | 
 | 746 |  | 
 | 747 |  | 
| Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 748 | Helper functions | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 749 | ---------------- | 
 | 750 |  | 
| Georg Brandl | 10430ad | 2009-09-26 20:59:11 +0000 | [diff] [blame] | 751 | .. function:: capwords(s, sep=None) | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 752 |  | 
| Ezio Melotti | a40bdda | 2009-09-26 12:33:22 +0000 | [diff] [blame] | 753 |    Split the argument into words using :meth:`str.split`, capitalize each word | 
 | 754 |    using :meth:`str.capitalize`, and join the capitalized words using | 
 | 755 |    :meth:`str.join`.  If the optional second argument *sep* is absent | 
 | 756 |    or ``None``, runs of whitespace characters are replaced by a single space | 
 | 757 |    and leading and trailing whitespace are removed, otherwise *sep* is used to | 
 | 758 |    split and join the words. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 759 |  |