In computer programming, string interpolation (or variable interpolation, variable substitution, or variable expansion) is the process of evaluating a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple template processing or, in formal terms, a form of quasi-quotation (or logic substitution interpretation). The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current context.

String interpolation is an alternative to building string via concatenation, which requires repeat quoting and unquoting; or substituting into a printf format string, where the variable is far from where it is used. Compare: apples = 4 puts "I have #{ apples} apples." # string interpolation puts "I have " + String(apples) + " apples." # string concatenation puts "I have %d apples." % apples # format string

String interpolation

Two types of literal expression are usually offered: one with interpolation enabled, the other without. Non-interpolated strings may also escape sequences, in which case they are termed a raw string, though in other cases this is separate, yielding three classes of raw string, non-interpolated (but escaped) string, interpolated (and escaped) string. For example, in Unix shells, single-quoted strings are raw, while double-quoted strings are interpolated. Placeholders are usually represented by a bare or a named sigil (typically $ or %), e.g. $apples or %apples, or with braces, e.g. { apples}, sometimes both, e.g. ${ apples}. In some cases additional formatting specifiers can be used (as in printf), e.g. { apples:3}, and in some cases the formatting specifiers themselves can be interpolated, e.g. { apples:width}. Expansion of the string usually occurs at run time.

String interpolation

Language support for string interpolation varies widely. Some languages do not offer string interpolation, instead using concatenation, simple formatting functions, or template libraries. String interpolation is common in many programming languages which make heavy use of string representations of data, such as Apache Groovy, Julia, Kotlin, Perl, PHP, Python, Ruby, Scala, Swift, Tcl and most Unix shells.

String interpolation

Algorithms

There are two main types of variable-expanding algorithms for variable interpolation: Replace and expand placeholders: creating a new string from the original one, by find–replace operations. Find variable reference (placeholder), replace it by its variable value. This algorithm offers no cache strategy. Split and join string: splitting the string into an array, merging it with the corresponding array of values, then joining items by concatenation. The split string can be cached for reuse.

Security issues

String interpolation, like string concatenation, may lead to security problems. If user input data is improperly escaped or filtered, the system will be exposed to SQL injection, script injection, XML external entity (XXE) injection, and cross-site scripting (XSS) attacks.

An SQL injection example: query = " " If $id is replaced with "'; ", executing this query will wipe out all the data in Table.

Examples

ABAP

DATA(apples) = 4. WRITE |I have { apples } apples|.The output will be:I have 4 apples

Bash

apples=4

echo "I have $apples apples" # or echo "I have ${ apples} apples" The output will be: I have 4 apples

Boo

apples = 4

print("I have $(apples) apples") # or print("I have { 0} apples" % apples) The output will be: I have 4 apples

C#

var apples = 4; var bananas = 3;

Console.WriteLine($"I have { apples} apples"); Console.WriteLine($"I have { apples + bananas} fruits");

The output will be: I have 4 apples I have 7 fruits

ColdFusion Markup Language

ColdFusion Markup Language (CFML) script syntax: apples = 4; writeOutput("I have #apples# apples");

Tag syntax: <cfset apples = 4> <cfoutput>I have #apples# apples</cfoutput>

The output will be:

CoffeeScript

apples = 4

console.log "I have #{ apples} apples" The output will be: I have 4 apples

Dart

int apples = 4, bananas = 3; print('I have $apples apples.'); print('I have ${ apples+bananas} fruits.'); The output will be: I have 4 apples. I have 7 fruits.

Go

, Go does not have string interpolation. There have been some proposals for string interpolation in the next version of the language, Go 2. Instead, Go uses printf format strings in the fmt.Sprintf function, string concatenation, or template libraries like text/template.