GDScript format strings

    Format strings are just like normal strings, except they contain certain placeholder character-sequences. These placeholders can then easily be replaced by parameters handed to the format string.

    As an example, with as a placeholder, the format string "Hello %s, how are you?" can easily be changed to "Hello World, how are you?". Notice the placeholder is in the middle of the string; modifying it without format strings could be cumbersome.

    Examine this concrete GDScript example:

    Placeholders always start with a %, but the next character or characters, the format specifier, determines how the given value is converted to a string.

    The %s seen in the example above is the simplest placeholder and works for most use cases: it converts the value by the same method by which an implicit String conversion or str() would convert it. Strings remain unchanged, Booleans turn into either "True" or "False", an integral or real number becomes a decimal, other types usually return their data in a human-readable string.

    There is also another way to format text in GDScript, namely the String.format() method. It replaces all occurrences of a key in the string with the corresponding value. The method can handle arrays or dictionaries for the key/value pairs.

    Arrays can be used as key, index, or mixed style (see below examples). Order only matters when the index or mixed style of Array is used.

    A quick example in GDScript:

    1. # Define a format string
    2. var format_string = "We're waiting for {str}"
    3. # Using the 'format' method, replace the 'str' placeholder
    4. var actual_string = format_string.format({"str": "Godot"})
    5. print(actual_string)
    6. # Output: "We're waiting for Godot"

    There are other , but they are only applicable when using the % operator.

    Multiple placeholders

    Format strings may contain multiple placeholders. In such a case, the values are handed in the form of an array, one value per placeholder (unless using a format specifier with *, see ):

    1. var format_string = "%s was reluctant to learn %s, but now he enjoys it."
    2. var actual_string = format_string % ["Estragon", "GDScript"]
    3. print(actual_string)
    4. # Output: "Estragon was reluctant to learn GDScript, but now he enjoys it."

    Note the values are inserted in order. Remember all placeholders must be replaced at once, so there must be an appropriate number of values.

    There are format specifiers other than s that can be used in placeholders. They consist of one or more characters. Some of them work by themselves like s, some appear before other characters, some only work with certain values or characters.

    One and only one of these must always appear as the last character in a format specifier. Apart from s, these require certain types of parameters.

    These characters appear before the above. Some of them work only under certain conditions.

    Padding

    The . (dot), * (asterisk), - (minus sign) and digit (0-9) characters are used for padding. This allows printing several values aligned vertically as if in a column, provided a fixed-width font is used.

    To pad a string to a minimum length, add an integer to the specifier:

    If the integer starts with 0, integral values are padded with zeroes instead of white space:

    1. print("%010d" % 12345)

    Precision can be specified for real numbers by adding a . (dot) with an integer following it. With no integer after ., a precision of 0 is used, rounding to integral value. The integer to use for padding must appear before the dot.

    1. # Pad to minimum length of 10, round to 3 decimal places
    2. print("%10.3f" % 10000.5555)
    3. # Output: " 10000.556"

    The - character will cause padding to the right rather than the left, useful for right text alignment:

    By using the * (asterisk) character, the padding or precision can be set without modifying the format string. It is used in place of an integer in the format specifier. The values for padding and precision are then passed when formatting:

    1. var format_string = "%*.*f"
    2. # Pad to length of 7, round to 3 decimal places:
    3. print(format_string % [7, 3, 8.8888])
    4. # Output: " 8.889"
    5. # 2 leading spaces

    It is still possible to pad with zeroes in integer placeholders by adding 0 before *:

    1. print("%0*d" % [2, 3])
    2. # Output: "03"

    To insert a literal % character into a format string, it must be escaped to avoid reading it as a placeholder. This is done by doubling the character:

    Format method examples

    The following are some examples of how to use the various invocations of the String.format method.

    Placeholders can also be customized when using String.format, here’s some examples of that functionality.

    Combining both the String.format method and the % operator could be useful, as String.format does not have a way to manipulate the representation of numbers.