github.com/hugorut/terraform@v1.1.3/website/docs/language/functions/format.mdx (about)

     1  ---
     2  page_title: format - Functions - Configuration Language
     3  description: |-
     4    The format function produces a string by formatting a number of other values
     5    according to a specification string.
     6  ---
     7  
     8  # `format` Function
     9  
    10  `format` produces a string by formatting a number of other values according
    11  to a specification string. It is similar to the `printf` function in C, and
    12  other similar functions in other programming languages.
    13  
    14  ```hcl
    15  format(spec, values...)
    16  ```
    17  
    18  ## Examples
    19  
    20  ```
    21  > format("Hello, %s!", "Ander")
    22  Hello, Ander!
    23  > format("There are %d lights", 4)
    24  There are 4 lights
    25  ```
    26  
    27  Simple format verbs like `%s` and `%d` behave similarly to template
    28  interpolation syntax, which is often more readable:
    29  
    30  ```
    31  > format("Hello, %s!", var.name)
    32  Hello, Valentina!
    33  > "Hello, ${var.name}!"
    34  Hello, Valentina!
    35  ```
    36  
    37  The `format` function is therefore more useful when you use more complex format
    38  specifications, as described in the following section.
    39  
    40  ## Specification Syntax
    41  
    42  The specification is a string that includes formatting verbs that are introduced
    43  with the `%` character. The function call must then have one additional argument
    44  for each verb sequence in the specification. The verbs are matched with
    45  consecutive arguments and formatted as directed, as long as each given argument
    46  is convertible to the type required by the format verb.
    47  
    48  The specification may contain the following verbs:
    49  
    50  | Verb  | Result                                                                                    |
    51  | ----- | ----------------------------------------------------------------------------------------- |
    52  | `%%`  | Literal percent sign, consuming no value.                                                 |
    53  | `%v`  | Default formatting based on the value type, as described below.                           |
    54  | `%#v` | JSON serialization of the value, as with `jsonencode`.                                    |
    55  | `%t`  | Convert to boolean and produce `true` or `false`.                                         |
    56  | `%b`  | Convert to integer number and produce binary representation.                              |
    57  | `%d`  | Convert to integer number and produce decimal representation.                             |
    58  | `%o`  | Convert to integer number and produce octal representation.                               |
    59  | `%x`  | Convert to integer number and produce hexadecimal representation with lowercase letters.  |
    60  | `%X`  | Like `%x`, but use uppercase letters.                                                     |
    61  | `%e`  | Convert to number and produce scientific notation, like `-1.234456e+78`.                  |
    62  | `%E`  | Like `%e`, but use an uppercase `E` to introduce the exponent.                            |
    63  | `%f`  | Convert to number and produce decimal fraction notation with no exponent, like `123.456`. |
    64  | `%g`  | Like `%e` for large exponents or like `%f` otherwise.                                     |
    65  | `%G`  | Like `%E` for large exponents or like `%f` otherwise.                                     |
    66  | `%s`  | Convert to string and insert the string's characters.                                     |
    67  | `%q`  | Convert to string and produce a JSON quoted string representation.                        |
    68  
    69  When `%v` is used, one of the following format verbs is chosen based on the value type:
    70  
    71  | Type      | Verb  |
    72  | --------- | ----- |
    73  | `string`  | `%s`  |
    74  | `number`  | `%g`  |
    75  | `bool`    | `%t`  |
    76  | any other | `%#v` |
    77  
    78  Null values produce the string `null` if formatted with `%v` or `%#v`, and
    79  cause an error for other verbs.
    80  
    81  A width modifier can be included with an optional decimal number immediately
    82  preceding the verb letter, to specify how many characters will be used to
    83  represent the value. Precision can be specified after the (optional) width
    84  with a period (`.`) followed by a decimal number. If width or precision are
    85  omitted then default values are selected based on the given value. For example:
    86  
    87  | Sequence | Result                       |
    88  | -------- | ---------------------------- |
    89  | `%f`     | Default width and precision. |
    90  | `%9f`    | Width 9, default precision.  |
    91  | `%.2f`   | Default width, precision 2.  |
    92  | `%9.2f`  | Width 9, precision 2.        |
    93  
    94  The following additional symbols can be used immediately after the `%` symbol
    95  to set additional flags:
    96  
    97  | Symbol | Result                                                         |
    98  | ------ | -------------------------------------------------------------- |
    99  | space  | Leave a space where the sign would be if a number is positive. |
   100  | `+`    | Show the sign of a number even if it is positive.              |
   101  | `-`    | Pad the width with spaces on the left rather than the right.   |
   102  | `0`    | Pad the width with leading zeros rather than spaces.           |
   103  
   104  By default, `%` sequences consume successive arguments starting with the first.
   105  Introducing a `[n]` sequence immediately before the verb letter, where `n` is a
   106  decimal integer, explicitly chooses a particular value argument by its
   107  one-based index. Subsequent calls without an explicit index will then proceed
   108  with `n`+1, `n`+2, etc.
   109  
   110  The function produces an error if the format string requests an impossible
   111  conversion or access more arguments than are given. An error is produced also
   112  for an unsupported format verb.
   113  
   114  -> **Note:** Width and precision modifiers with non-numeric types such as
   115  strings (`%s`) are interpreted differently. Setting either width or precision to
   116  zero is the same as not including them at all.
   117  
   118  ## Related Functions
   119  
   120  * [`formatdate`](/language/functions/formatdate) is a specialized formatting function for
   121    human-readable timestamps.
   122  * [`formatlist`](/language/functions/formatlist) uses the same specification syntax to
   123    produce a list of strings.