GDScript has two ways to put values into text: the % operator and the format() method. % is the one you see most:
Ana has 42 HP and 13.50 gold
Score: 900
Each placeholder takes the next value from the array on the right. With one placeholder, you can pass the value without an array.
Placeholders
| Placeholder | Shows | "…" % 255 |
|---|---|---|
%s | any value as text | 255 |
%d | a whole number | 255 |
%f | a float, 6 decimals | 255.000000 |
%.2f | a float, 2 decimals | 255.00 |
%x, %X | hexadecimal | ff, FF |
%o | octal | 377 |
%c | the character with this code | ÿ |
%% | a literal % |
%s works with every type, including arrays, vectors and null.
Width and Padding
A number between % and the letter sets the minimum width. - pads on the right, 0 fills with zeros, and + always shows the sign:
[ 42]
[42 ]
[00042]
[+42]
[ 3.142]
75%
Padding lines up a score table in the Output panel:
Ana 1200
Bo 85
Chidi 430
format() with Named Placeholders
format() replaces {0}, {1} with array items, or {name} with dictionary keys:
Ana beat Bo
Mirror vs Mirror
Ana reached level 7
Named placeholders keep long messages readable and let one value appear many times.
Rounding a Float for Display
Formatting and rounding are different jobs:
0.66666666666667
0.67
0.67
0.667
0.67
Use "%.2f" for what the player sees, such as "%.1f s" on a timer label. Use snapped() when you need the rounded number for more math.
Common Mistakes
- Fewer values than placeholders.
"%s and %s" % ["a"]prints the errorString formatting error: not enough arguments for format string.and gives back the text unformatted, so the player sees%s and %s. - Passing an array to one
%s."%s" % [1, 2]fails the same way, withnot all arguments converted during string formatting., because the array is read as two values. Wrap it:"%s" % [[1, 2]]. - Joining numbers with
+."HP: " + hpis an error. Use"HP: %d" % hpor"HP: " + str(hp).
Frequently Asked Questions
How do I format a string in GDScript?
Use the % operator: "%s has %d HP" % [player_name, hp]. Each placeholder takes the next value from the array. With a single value you can skip the array: "Score: %d" % score.
How do I show a float with 2 decimal places in Godot?
Use "%.2f" % value, which gives text such as "3.14". To round the number itself, use snapped(value, 0.01). Formatting is the right choice for labels, because it always shows exactly two digits.
Does GDScript have f-strings?
No. GDScript has no f-strings or ${} interpolation. Use % formatting or "{name}".format({"name": value}) instead.
How do I print a percent sign with % formatting?
Write it twice: "%d%%" % 50 gives "50%". A single % followed by a letter is read as a placeholder.