Skip to content

Commit 5606be5

Browse files
committed
Improve GDScript format strings tutorial
Remove usages of "integral". Add section on string concatenation. Improve organization. Fix incorrect examples.
1 parent a178d0c commit 5606be5

File tree

1 file changed

+95
-55
lines changed

1 file changed

+95
-55
lines changed

tutorials/scripting/gdscript/gdscript_format_string.rst

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33
GDScript format strings
44
=======================
55

6-
GDScript offers a feature called *format strings*, which allows reusing text
7-
templates to succinctly create different but similar strings.
6+
Godot offers multiple ways to dynamically change the contents of strings:
87

9-
Format strings are just like normal strings, except they contain certain
10-
placeholder character-sequences. These placeholders can then easily be replaced
11-
by parameters handed to the format string.
8+
- Format strings: ``var string = "I have %s cats." % "3"``
9+
- The ``String.format()`` method: ``var string = "I have {} cats.".format([3])``
10+
- String concatenation: ``var string = "I have " + str(3) + " cats."``
1211

13-
As an example, with ``%s`` as a placeholder, the format string ``"Hello %s, how
14-
are you?"`` can easily be changed to ``"Hello World, how are you?"``. Notice
15-
the placeholder is in the middle of the string; modifying it without format
16-
strings could be cumbersome.
12+
This page explains how to use format strings, and briefly explains the ``format()``
13+
method and string concatenation.
1714

15+
Format strings
16+
--------------
1817

19-
Usage in GDScript
20-
-----------------
18+
*Format strings* are a way to reuse text templates to succinctly create different
19+
but similar strings.
20+
21+
Format strings are just like normal strings, except they contain certain
22+
placeholder character sequences such as ``%s``. These placeholders can then
23+
be replaced by parameters handed to the format string.
2124

2225
Examine this concrete GDScript example:
2326

@@ -38,34 +41,12 @@ string.
3841

3942
The ``%s`` seen in the example above is the simplest placeholder and works for
4043
most use cases: it converts the value by the same method by which an implicit
41-
String conversion or ``str()`` would convert it. Strings remain unchanged,
42-
Booleans turn into either ``"True"`` or ``"False"``, an integral or real number
43-
becomes a decimal, other types usually return their data in a human-readable
44-
string.
45-
46-
There is also another way to format text in GDScript, namely the ``String.format()``
47-
method. It replaces all occurrences of a key in the string with the corresponding
48-
value. The method can handle arrays or dictionaries for the key/value pairs.
49-
50-
Arrays can be used as key, index, or mixed style (see below examples). Order only
51-
matters when the index or mixed style of Array is used.
52-
53-
A quick example in GDScript:
54-
55-
::
56-
57-
# Define a format string
58-
var format_string = "We're waiting for {str}"
59-
60-
# Using the 'format' method, replace the 'str' placeholder
61-
var actual_string = format_string.format({"str": "Godot"})
62-
63-
print(actual_string)
64-
# Output: "We're waiting for Godot"
65-
66-
There are other `format specifiers`_, but they are only applicable when using
67-
the ``%`` operator.
44+
String conversion or :ref:`str() <class_@GlobalScope_method_str>` would convert
45+
it. Strings remain unchanged, booleans turn into either ``"True"`` or ``"False"``,
46+
an ``int`` or ``float`` becomes a decimal, and other types usually return their data
47+
in a human-readable string.
6848

49+
There are other `format specifiers`_.
6950

7051
Multiple placeholders
7152
---------------------
@@ -108,19 +89,19 @@ specifier. Apart from ``s``, these require certain types of parameters.
10889
| ``c`` | A single **Unicode character**. Expects an unsigned 8-bit integer |
10990
| | (0-255) for a code point or a single-character string. |
11091
+-------+---------------------------------------------------------------------+
111-
| ``d`` | A **decimal integral** number. Expects an integral or real number |
92+
| ``d`` | A **decimal integer**. Expects an integer or a real number |
11293
| | (will be floored). |
11394
+-------+---------------------------------------------------------------------+
114-
| ``o`` | An **octal integral** number. Expects an integral or real number |
95+
| ``o`` | An **octal integer**. Expects an integer or a real number |
11596
| | (will be floored). |
11697
+-------+---------------------------------------------------------------------+
117-
| ``x`` | A **hexadecimal integral** number with **lower-case** letters. |
118-
| | Expects an integral or real number (will be floored). |
98+
| ``x`` | A **hexadecimal integer** with **lower-case** letters. |
99+
| | Expects an integer or a real number (will be floored). |
119100
+-------+---------------------------------------------------------------------+
120-
| ``X`` | A **hexadecimal integral** number with **upper-case** letters. |
121-
| | Expects an integral or real number (will be floored). |
101+
| ``X`` | A **hexadecimal integer** with **upper-case** letters. |
102+
| | Expects an integer or a real number (will be floored). |
122103
+-------+---------------------------------------------------------------------+
123-
| ``f`` | A **decimal real** number. Expects an integral or real number. |
104+
| ``f`` | A **decimal real** number. Expects an integer or a real number. |
124105
+-------+---------------------------------------------------------------------+
125106
| ``v`` | A **vector**. Expects any float or int-based vector object ( |
126107
| | ``Vector2``, ``Vector3``, ``Vector4``, ``Vector2i``, ``Vector3i`` or|
@@ -149,7 +130,7 @@ conditions.
149130
+---------+-------------------------------------------------------------------+
150131
| ``-`` | **Pad to the right** rather than the left. |
151132
+---------+-------------------------------------------------------------------+
152-
| ``*`` | **Dynamic padding**, expect additional integral parameter to set |
133+
| ``*`` | **Dynamic padding**, expects additional integer parameter to set |
153134
| | padding or precision after ``.``, see `dynamic padding`_. |
154135
+---------+-------------------------------------------------------------------+
155136

@@ -170,7 +151,7 @@ To pad a string to a minimum length, add an integer to the specifier:
170151
# output: " 12345"
171152
# 5 leading spaces for a total length of 10
172153

173-
If the integer starts with ``0``, integral values are padded with zeroes
154+
If the integer starts with ``0``, integer values are padded with zeroes
174155
instead of white space:
175156

176157
::
@@ -180,7 +161,7 @@ instead of white space:
180161

181162
Precision can be specified for real numbers by adding a ``.`` (*dot*) with an
182163
integer following it. With no integer after ``.``, a precision of 0 is used,
183-
rounding to integral value. The integer to use for padding must appear before
164+
rounding to integer values. The integer to use for padding must appear before
184165
the dot.
185166

186167
::
@@ -238,12 +219,36 @@ avoid reading it as a placeholder. This is done by doubling the character:
238219
# Output: "Remaining health: 56%"
239220

240221

222+
String format method
223+
--------------------
224+
225+
There is also another way to format text in GDScript, namely the
226+
:ref:`String.format() <class_String_method_format>`
227+
method. It replaces all occurrences of a key in the string with the corresponding
228+
value. The method can handle arrays or dictionaries for the key/value pairs.
229+
230+
Arrays can be used as key, index, or mixed style (see below examples). Order only
231+
matters when the index or mixed style of Array is used.
232+
233+
A quick example in GDScript:
234+
235+
::
236+
237+
# Define a format string
238+
var format_string = "We're waiting for {str}"
239+
240+
# Using the 'format' method, replace the 'str' placeholder
241+
var actual_string = format_string.format({"str": "Godot"})
242+
243+
print(actual_string)
244+
# Output: "We're waiting for Godot"
245+
246+
241247
Format method examples
242-
----------------------
248+
~~~~~~~~~~~~~~~~~~~~~~
243249

244250
The following are some examples of how to use the various invocations of the
245-
``String.format`` method.
246-
251+
``String.format()`` method.
247252

248253
+------------+-----------+------------------------------------------------------------------------------+-------------------+
249254
| **Type** | **Style** | **Example** | **Result** |
@@ -258,9 +263,9 @@ The following are some examples of how to use the various invocations of the
258263
+------------+-----------+------------------------------------------------------------------------------+-------------------+
259264
| Array | index | ``"Hi, {0} v{1}!".format(["Godette","3.0"])`` | Hi, Godette v3.0! |
260265
+------------+-----------+------------------------------------------------------------------------------+-------------------+
261-
| Array | mix | ``"Hi, {name} v{0}!".format([3.0, ["name","Godette"]])`` | Hi, Godette v3.0! |
266+
| Array | mix | ``"Hi, {name} v{0}!".format(["3.0", ["name","Godette"]])`` | Hi, Godette v3.0! |
262267
+------------+-----------+------------------------------------------------------------------------------+-------------------+
263-
| Array | no index | ``"Hi, {} v{}!".format(["Godette", 3.0], "{}")`` | Hi, Godette v3.0! |
268+
| Array | no index | ``"Hi, {} v{}!".format(["Godette", "3.0"], "{}")`` | Hi, Godette v3.0! |
264269
+------------+-----------+------------------------------------------------------------------------------+-------------------+
265270

266271
Placeholders can also be customized when using ``String.format``, here's some
@@ -286,5 +291,40 @@ Combining both the ``String.format`` method and the ``%`` operator could be usef
286291
| ``"Hi, {0} v{version}".format({0:"Godette", "version":"%0.2f" % 3.114})`` | Hi, Godette v3.11 |
287292
+---------------------------------------------------------------------------+-------------------+
288293

289-
In Godot's C++ code, GDScript format strings can be accessed using the
290-
``vformat`` helper function in the :ref:`Variant<class_Variant>` header.
294+
String concatenation
295+
--------------------
296+
297+
You can also combine strings by *concatenating* them together, using the ``+``
298+
operator.
299+
300+
::
301+
302+
# Define a base string
303+
var base_string = "We're waiting for "
304+
305+
# Concatenate the string
306+
var actual_string = base_string + "Godot"
307+
308+
print(actual_string)
309+
# Output: "We're waiting for Godot"
310+
311+
When using string concatenation, values that are not strings must be converted using
312+
the ``str()`` function. There is no way to specify the string format of converted
313+
values.
314+
315+
::
316+
317+
var name_string = "Godette"
318+
var version = 3.0
319+
var actual_string = "Hi, " + name_string + " v" + str(version) + "!"
320+
321+
print(actual_string)
322+
# Output: "Hi, Godette v3!"
323+
324+
Because of these limitations, format strings or the ``format()`` method are often
325+
a better choice. In many cases, string concatenation is also less readable.
326+
327+
.. note::
328+
329+
In Godot's C++ code, GDScript format strings can be accessed using the
330+
``vformat()`` helper function in the :ref:`Variant<class_Variant>` header.

0 commit comments

Comments
 (0)