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[1] 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;[2] or substituting into a printf format string, where the variable is far from where it is used. Compare:
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.
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.
There are two main types of variable-expanding algorithms for variable interpolation:[3]
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.[4]
An SQL injection example: query = " "If $id
is replaced with "'; {{code|2=sql|1=DELETE FROM Table; SELECT * FROM Table WHERE id='}}
", executing this query will wipe out all the data in Table
.
See main article: ABAP.
See main article: Bash (Unix shell).
echo "I have $ apples"The output will be:
See main article: Boo (programming language).
print("I have apples" % apples)The output will be:
See main article: C Sharp (programming language).
Console.WriteLine($"I have apples");Console.WriteLine($"I have fruits");[5]
The output will be:
See main article: article and ColdFusion Markup Language.
ColdFusion Markup Language (CFML) script syntax:
Tag syntax:
The output will be:
See main article: CoffeeScript.
See main article: Dart (programming language).
See main article: Go (programming language)., Go does not have string interpolation. There have been some proposals for string interpolation in the next version of the language, Go 2.[6] [7] Instead, Go uses printf format strings in the fmt.Sprintf
function, string concatenation, or template libraries like text/template
.
See main article: Groovy (programming language). In groovy, interpolated strings are known as GStrings:[8]
See main article: Haxe.
See main article: article and Java (programming language). Java 21 does have interpolated strings thanks to JEP 430. In jshell you could use the constant STR of java.lang.StringTemplate directly.
https://openjdk.org/jeps/430
See main article: article and JavaScript. JavaScript, as of the ECMAScript 2015 (ES6) standard, supports string interpolation using backticks ``
. This feature is called template literals.[10] Here is an example:
Template literals can also be used for multi-line strings:
The output will be:
See main article: Julia (programming language).
See main article: Kotlin (programming language).
See main article: Nemerle.
See main article: Nim (programming language). Nim provides string interpolation via the strutils module.Formatted string literals inspired by Python F-string are provided via the strformat module,the strformat macro verifies that the format string is well-formed and well-typed,and then are expanded into Nim source code at compile-time.
echo fmt"""I have apples"""
echo fmt"I have apples"
echo fmt("I have (apples) ", '(', ')')
echo fmt"""""" The output will be:
See main article: Nix package manager.
See main article: ParaSail (programming language).
See main article: Perl.
See main article: PHP.
See main article: Python (programming language). Python supports string interpolation as of version 3.6, referred to as "formatted string literals".[11] [12] [13] Such a literal begins with an f
or F
before the opening quote, and uses braces for placeholders:
See main article: Ruby (programming language).
See main article: Crystal (programming language).
puts "I have %s apples" % applesputs "I have % apples" %
The output will be:
See main article: article and Rust (programming language).
Rust does not have general string interpolation, but provides similar functionality via macros, referred to as "Captured identifiers in format strings", introduced in version 1.58.0, released 2022-01-13.[14]
Rust provides formatting via the std::fmt module, which is interfaced with through various macros such as format!, write!, and print!. These macros are converted into Rust source code at compile-time, whereby each argument interacts with a formatter. The formatter supports positional parameters, named parameters, argument types, defining various formatting traits, and capturing identifiers from the environment.
See main article: article and Scala (programming language).
Scala 2.10+ provides a general facility to allow arbitrary processing of a string literal, and supports string interpolation using the included s
and f
string interpolators. It is also possible to write custom ones or override the standard ones.
The f
interpolator is a compiler macro that rewrites a format string with embedded expressions as an invocation of String.format. It verifies that the format string is well-formed and well-typed.
Scala 2.10+'s string interpolation allows embedding variable references directly in processed string literals. Here is an example:
The output will be:
In Sciter any function with name starting from $ is considered as interpolating function and so interpolation is customizable and context sensitive:
domElement.$content(
I have apples
);domElement.$append(I have fruits
);Where I have apples I have " + apples.toHtmlString + " apples
See main article: article and SNOBOL.
See main article: article and Swift (programming language).
In Swift, a new String value can be created from a mix of constants, variables, literals, and expressions by including their values inside a string literal.[15] Each item inserted into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.
See main article: article and Tcl.
The Tool Command Language has always supported string interpolation in all quote-delimited strings.
In order to actually format - and not simply replace - the values, there is a formatting function.
See main article: article and TypeScript.
As of version 1.4, TypeScript supports string interpolation using backticks ``
. Here is an example:console.log
function can be used as a printf
function. The above example can be rewritten, thusly:
As of Visual Basic 14, string interpolation is supported in Visual Basic.[16]
The output will be: