Menu

GDScript String Formatting: % Operator and format()

Build text from values in GDScript with the % operator (%s, %d, %f, padding and decimals) and with String.format() placeholders, plus how to round a float for display.

This page includes runnable editors. Edit, run, and see output instantly.

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

PlaceholderShows"…" % 255
%sany value as text255
%da whole number255
%fa float, 6 decimals255.000000
%.2fa float, 2 decimals255.00
%x, %Xhexadecimalff, FF
%ooctal377
%cthe 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 error String 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, with not all arguments converted during string formatting., because the array is read as two values. Wrap it: "%s" % [[1, 2]].
  • Joining numbers with +. "HP: " + hp is an error. Use "HP: %d" % hp or "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.

Coddy programming languages illustration

Learn to code with Coddy

Get started