« Back to Glossary Index

Usually used in the sense of “string interpolation”, which is what we call it when you substitute something, like the value of a variable, into a string. It’s also technically a mathematical term used when getting a value between two known points (e.g. on a graph), but we rarely use that definition for creating Visual Novels.

In Ren’Py, string interpolation is handled with square brackets []. So, consider the following code:

default name = "Fen"
label start():
    "Hello, [name]!"

When the player starts the game, they won’t see the dialogue "Hello, [name]!" – instead, Ren’Py recognizes that name is a variable, because it’s inside square brackets. Because the value of name is "Fen", the player will instead see "Hello, Fen!".

While you should almost always use [] for interpolating variables in dialogue and in things like screens, two other Python-specific interpolation methods are %-formatting, the format function, and in Python 3.9 and Ren’Py 8.0+ only, f-strings .

% formatting is generally more finicky than the other types, so I suggest you use pretty much any other interpolation method. In practice, it looks like "Your name is %s" % name. You need to specify the type of the variable you’re interpolating – in this case, it’s a string, so we use %s. If you wanted to interpolate an integer, for example, you’d use %d instead. This format does not work for dialogue (so, its only real uses are within Python functions or occasionally inside screens).

The format function can be used on any non-dialogue string and allows you to use curly braces {} to insert variables. It looks like "Your name is {}".format(name). You do not need to specify the variable type. You can specify formatting inside the braces to do things like ensure a number has two decimal places. This format does not work for dialogue (so again, its only real uses are within Python functions or inside screens).

In Python 3.6+, which is used in Ren’Py 8.0+, you can also use “f-strings” to interpolate data into strings. This is pretty much the way Ren’Py does string interpolation, except with curly braces instead of square ones and an f preceding the string. It looks like f"Your name is {name}". This format does not work for dialogue, though it can be useful for screens and inside Python functions due to its ability to put logic right inside the curly braces e.g. f"You are {current_health/max_health} percent healthy." where the calculation current_health / max_health is done right inside the string.

Finally, for historical interest, you may occasionally see “old-style substitution”. In Ren’Py versions prior to 6.13 (which was released in Sept 2011), a special version of %-style substitution was used that looked like "Your name is %(name)s." where the variable name was preceded by %( and followed by )s. This method remains usable in modern Ren’Py, though square brackets should always be used in preference to it for new code.

variable substitution, string interpolation, variable interpolation
« Back to Glossary Index