2
0

gdscript_format_string.rst 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. .. _doc_gdscript_printf:
  2. GDScript format strings
  3. =======================
  4. GDScript offers a feature called *format strings* which allows reusing text
  5. templates to succinctly create different but similar strings.
  6. Format strings are just like normal strings, except they contain certain
  7. placeholder character-sequences. These placeholders can then easily be replaced
  8. by parameters handed to the format string.
  9. As an example, with ``%s`` as a placeholder, the format string ``"Hello %s, how
  10. are you?`` can easily be changed to ``"Hello World, how are you?"``. Notice
  11. the placeholder is in the middle of the string; modifying it without format
  12. strings could be cumbersome.
  13. Usage in GDScript
  14. -----------------
  15. Examine this concrete GDScript example::
  16. # Define a format string with placeholder '%s'
  17. var format_string = "We're waiting for %s."
  18. # Using the '%' operator, the placeholder is replaced with the desired value
  19. var actual_string = format_string % "Godot"
  20. print(actual_string)
  21. # output: "We're waiting for Godot."
  22. Placeholders always start with a ``%``, but the next character or characters,
  23. the *format specifier*, determines how the given value is converted to a
  24. string.
  25. The ``%s`` seen in the example above is the simplest placeholder and works for
  26. most use cases: it converts the value by the same method by which an implicit
  27. String conversion or ``str()`` would convert it. Strings remain unchanged,
  28. Booleans turn into either ``"True"`` or ``"False"``, an integral or real number
  29. becomes a decimal, other types usually return their data in a human-readable
  30. string.
  31. There are other `format specifiers`_.
  32. Multiple placeholders
  33. ---------------------
  34. Format strings may contain multiple placeholders. In such a case, the values
  35. are handed in the form of an array, one value per placeholder (unless using a
  36. format specifier with ``*``, see `dynamic padding`_)::
  37. var format_string = "%s was reluctant to learn %s, but now he enjoys it."
  38. var actual_string = format_string % ["Estragon", "GDScript"]
  39. print(actual_string)
  40. # output: "Estragon was reluctant to learn GDScript, but now he enjoys it."
  41. Note the values are inserted in order. Remember all placeholders must be
  42. replaced at once, so there must be an appropriate number of values.
  43. Format specifiers
  44. -----------------
  45. There are format specifiers other than ``s`` that can be used in placeholders.
  46. They consist of one or more characters. Some of them work by themselves like
  47. ``s``, some appear before other characters, some only work with certain
  48. values or characters.
  49. Placeholder types
  50. ~~~~~~~~~~~~~~~~~
  51. One and only one of these must always appear as the last character in a format
  52. specifier. Apart from ``s``, these require certain types of parameters.
  53. +-------+---------------------------------------------------------------------+
  54. | ``s`` | **Simple** conversion to String by the same method as implicit |
  55. | | String conversion. |
  56. +-------+---------------------------------------------------------------------+
  57. | ``c`` | A single **Unicode character**. Expects an unsigned 8-bit integer |
  58. | | (0-255) for a code point or a single-character string. |
  59. +-------+---------------------------------------------------------------------+
  60. | ``d`` | A **decimal integral** number. Expects an integral or real number |
  61. | | (will be floored). |
  62. +-------+---------------------------------------------------------------------+
  63. | ``o`` | An **octal integral** number. Expects an integral or real number |
  64. | | (will be floored). |
  65. +-------+---------------------------------------------------------------------+
  66. | ``x`` | A **hexadecimal integral** number with **lower-case** letters. |
  67. | | Expects an integral or real number (will be floored). |
  68. +-------+---------------------------------------------------------------------+
  69. | ``X`` | A **hexadecimal integral** number with **upper-case** letters. |
  70. | | Expects an integral or real number (will be floored). |
  71. +-------+---------------------------------------------------------------------+
  72. | ``f`` | A **decimal real** number. Expects an integral or real number. |
  73. +-------+---------------------------------------------------------------------+
  74. Placeholder modifiers
  75. ~~~~~~~~~~~~~~~~~~~~~
  76. These characters appear before the above. Some of them work only under certain
  77. conditions.
  78. +---------+-------------------------------------------------------------------+
  79. | ``+`` | In number specifiers, **show + sign** if positive. |
  80. +---------+-------------------------------------------------------------------+
  81. | Integer | Set **padding**. Padded with spaces or with zeroes if integer |
  82. | | starts with ``0`` in an integer placeholder. When used after |
  83. | | ``.``, see ``.``. |
  84. +---------+-------------------------------------------------------------------+
  85. | ``.`` | Before ``f``, set **precision** to 0 decimal places. Can be |
  86. | | followed up with numbers to change. Padded with zeroes. |
  87. +---------+-------------------------------------------------------------------+
  88. | ``-`` | **Pad to the right** rather than the left. |
  89. +---------+-------------------------------------------------------------------+
  90. | ``*`` | **Dynamic padding**, expect additional integral parameter to set |
  91. | | padding or precision after ``.``, see `dynamic padding`_. |
  92. +---------+-------------------------------------------------------------------+
  93. Padding
  94. -------
  95. The ``.`` (*dot*), ``*`` (*asterisk*), ``-`` (*minus sign*) and digit
  96. (``0``-``9``) characters are used for padding. This allows printing several
  97. values aligned vertically as if in a column, provided a fixed-width font is
  98. used.
  99. To pad a string to a minimum length, add an integer to the specifier::
  100. print("%10d" % 12345)
  101. # output: " 12345"
  102. # 5 leading spaces for a total length of 10
  103. If the integer starts with ``0``, integral values are padded with zeroes
  104. instead of white space::
  105. print("%010d" % 12345)
  106. # output: "0000012345"
  107. Precision can be specified for real numbers by adding a ``.`` (*dot*) with an
  108. integer following it. With no integer after ``.``, a precision of 0 is used,
  109. rounding to integral value. The integer to use for padding must appear before
  110. the dot.
  111. ::
  112. # pad to minimum length of 10, round to 3 decimal places
  113. print("%10.3f" % 10000.5555)
  114. # output: " 10000.556"
  115. # 1 leading space
  116. The ``-`` character will cause padding to the right rather than the left,
  117. useful for right text alignment::
  118. print("%-10d" % 12345678)
  119. # output: "12345678 "
  120. # 2 trailing spaces
  121. Dynamic padding
  122. ~~~~~~~~~~~~~~~
  123. By using the ``*`` (*asterisk*) character, the padding or precision can be set
  124. without modifying the format string. It is used in place of an integer in the
  125. format specifier. The values for padding and precision are then passed when
  126. formatting::
  127. var format_string = "%*.*f"
  128. # pad to length of 7, round to 3 decimal places:
  129. print(format_string % [7, 3, 8.8888])
  130. # output: " 8.889"
  131. # 2 leading spaces
  132. It is still possible to pad with zeroes in integer placeholders by adding ``0``
  133. before ``*``::
  134. print("%0*d" % [2, 3])
  135. #output: "03"
  136. Escape sequence
  137. ---------------
  138. To insert a literal ``%`` character into a format string, it must be escaped to
  139. avoid reading it as a placeholder. This is done by doubling the character::
  140. var health = 56
  141. print("Remaining health: %d%%" % health)
  142. # output: "Remaining health: 56%"