String interpolation
In computer programming, string interpolation 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. String interpolation allows easier and more intuitive string formatting and content-specification compared with string concatenation.
String interpolation is common in many programming languages which make heavy use of string representations of data, such as Apache Groovy, Kotlin, Perl, PHP, Python, Ruby, Scala, Swift, Tcl and most Unix shells. Two modes of literal expression are usually offered: one with interpolation enabled, the other without. Placeholders are usually represented by a bare or a named sigil, e.g.
$placeholder
or %123
. Expansion of the string usually occurs at run time.Variations
Some languages do not offer string interpolation, instead offering a standard function where one parameter is the printf format string, and other provide the values for each placeholder.Ruby uses the
#
symbol for interpolation, and allows interpolating any expression, not only variables. Other languages may support more advanced interpolation with a special formatting function, such as printf
, in which the first argument, the format, specifies the pattern in which the remaining arguments are substituted.Algorithms
There are two main types of expand variable algorithms for variable interpolation:- Replace and expand placeholders: creating a new string from the original one, by find-replace operations. Find variable-reference, replace it by its variable-value. This algorithm offers no cache strategy.
- Split and join string: splitting the string into an array, and merging it with the corresponding array of values; then join items by concatenation. The split string can be cached to reuse.
Security issues
An SQL injection example:
query = " "
If
$id
is replaced with "';
", executing this query will wipe out all the data in Table.Examples
The following Perl code works identically in PHP:$name = "Alice";
print "$ said Hello World to the crowd of people.";
produces the output:
Alice said Hello World to the crowd of people.
Bash
echo "I have $apples apples"
- or
The output will be:
Boo
- or
The output will be:
C#
var apples = 4;
var bananas = 3;
// Before C# 6.0
Console.WriteLine;
Console.WriteLine;
// C# 6.0
Console.WriteLine;
Console.WriteLine;
The output will be:
I have 4 apples
I have 7 fruit
ColdFusion Markup Language
script syntax:apples = 4;
writeOutput;
Tag syntax:
The output will be:
CoffeeScript
console.log "I have # apples"
The output will be:
Dart
print;
print;
The output will be:
I have 7 fruit.
Groovy
In groovy, interpolated strings are known as GStrings:def sentence = "A developer is a $"
print sentence
The output will be:
Haxe
var bananas = 3;
trace;
trace;
The output will be:
I have 7 fruit.
Java
In Java versions 5 and above, the static methodString.format
can be used for interpolation:int apples = 4;
int bananas = 3;
System.out.println;
System.out.println;
In Java version 1.1 and above, the
MessageFormat
class can format sets of objects using placeholders:Object testArgs = ;
MessageFormat form = new MessageFormat;
System.out.println;
JavaScript
, as of the ECMAScript 2015 standard, supports string interpolation using backticks``
. This feature is called template literals. Here is an example:var apples = 4;
var bananas = 3;
console.log;
console.log;
The output will be:
I have 4 apples
I have 7 fruit
Julia
apples = 4
bananas = 3
The output will be:
I have 4 apples and 3 bananas, making 7 pieces of fruit in total.
Kotlin
val apples = 4
val bananas = 3
val sentence = "A developer is a $quality. I have $ fruit"
println
The output will be:
Nemerle
def bananas = 3;
Console.WriteLine;
Console.WriteLine;
It also supports advanced formatting features, such as:
Console.WriteLine;
The output will be:
bananas
Next Generation Shell
The recommended syntax is$
though $var
is also supported:apples = 4
bananas = 3
sentence = "A developer is a $quality. I have $ fruit"
echo
The output will be:
ParaSail
const Bananas := 3
Println
Println
The output will be:
I have 4 apples.
I have 7 fruit.
Perl
my $bananas = 3;
print "I have $apples apples.\n";
print "I have @ fruit.\n"; # Uses the Perl array interpolation.
The output will be:
I have 4 apples.
I have 7 fruit.
PHP
$bananas = 3;
echo "There are $apples apples and $bananas bananas.";
echo "\n";
echo "I have $ apples and $ bananas.";
I have 5 apples and 3 bananas.
Python
- in all versions
print # no longer recommended
printd apples" % locals) # no longer recommended
- with Python 2.6+
- with Python 2.7+
- or with Python 3.6+
The output will be:
I have 4 apples
Ruby / Crystal
puts "I have # apples"
- or
- or
The output will be:
Rust
Rust provides string interpolation via the module, which is interfaced with through various macros such as , , and . These macros are converted into Rust source code at compile-time, whereby each argument interacts with a . The formatter supports , , , and defining various .let = ;
println!;
There are 4 apples and 3 bananas.
Scala
2.10+ has implemented the following string interpolators: s, f and raw. 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.
The standard interpolators
Scala 2.10+'s string interpolation allows embedding variable references directly in processed string literals. Here is an example:val apples = 4
val bananas = 3
//before Scala 2.10
printf
println
//Scala 2.10+
println
println
println
The output will be:
Sciter (tiscript)
In Sciter any function with name starting from $ is considered as interpolating function and so interpolation is customizable and context sensitive:var apples = 4
var bananas = 3
var domElement =...;
domElement.$content;
domElement.$append;
Where
I have " + apples.toHtmlString + " apples
";Swift
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. Each item inserted into the string literal is wrapped in a pair of parentheses, prefixed by a backslash.Tcl
The Tool Command Language has always supported string interpolation in all quote-delimited strings.set apples 4
puts "I have $apples apples."
The output will be:
In order to actually format - and not simply replace - the values, there is a formatting function.
set apples 4
puts
TypeScript
As of version 1.4, TypeScript supports string interpolation using backticks``
. Here is an example:var apples: number = 4;
console.log;
The output will be:
The
console.log
function can be used as a printf
function. The above example can be rewritten, thusly:var apples: number = 4;
console.log;
The output remains the same.
Visual Basic
As of Visual Basic 14, String Interpolation is supported in Visual Basic.name = "Tom"
Console.WriteLine
will print "Hello, Tom".