String interpolation

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). "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 (termed "raw string"). Placeholders are usually represented by a bare or a named sigil (typically codice_1 or codice_2), e.g. codice_3 or codice_4. 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(s) provide the values for each placeholder.
Ruby uses the codice_5 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 codice_6, 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":
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 Injection (XXE), and cross-site scripting (XSS) attacks.
An SQL injection example:
 query = " "
If "codice_7" is replaced with "codice_8", executing this query will wipe out all the data in Table.
Examples.
The following Perl code works identically in PHP:
$name = "Alice";
print "${name} said Hello World to the crowd of people.";
produces the output: codice_9
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"
echo "I have ${apples} apples"
The output will be:
I have 4 apples
Boo.
apples = 4
print("I have $(apples) apples")
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} fruit.');
The output will be:
I have 4 apples.
I have 7 fruit.
Go.
lang := "Golang"
apples := 3
fmt.Printf("I am a %s developer.\n", lang)
fmt.Printf("I have %d apples.\n", apples)
The output will be:
I am a Golang developer.
I have 3 apples.
Groovy.
In groovy, interpolated strings are known as GStrings:
def quality = "superhero"
final age = 52
def sentence = "A developer is a $quality, if he is ${age <= 42 ? "young" : "seasoned"}"
println sentence
The output will be:
A developer is a superhero if he is seasoned
Haxe.
var apples = 4;
var bananas = 3;
trace('I have $apples apples.');
trace('I have ${apples+bananas} fruit.');
The output will be:
I have 4 apples.
I have 7 fruit.
Java.
Lacking true interpolated strings, Java uses helper functions as a workaround. 
In Java versions 5 and above, the static method codice_10 can be used for interpolation:
int apples = 4;
int bananas = 3;
System.out.println(String.format("I have %s apples and %s bananas", apples, bananas));
System.out.println(String.format("I have %s fruit", apples + bananas));
In Java version 1.1 and above, the codice_11 class can format sets of objects using placeholders:
Object[] testArgs = {Long.valueOf(3), "MyDisk"};
MessageFormat form = new MessageFormat(
 "The disk \"{1}\" contains {0} file(s).");
System.out.println(form.format(testArgs));
JavaScript.
JavaScript, as of the ECMAScript 2015 (ES6) standard, supports string interpolation using backticks codice_12. This feature is called "template literals". Here is an example:
var apples = 4;
var bananas = 3;
console.log(`I have ${apples} apples`);
console.log(`I have ${apples + bananas} fruit`);
The output will be:
I have 4 apples
I have 7 fruit
Julia.
apples = 4
bananas = 3
print("I have $apples apples and $bananas bananas, making $(apples + bananas) pieces of fruit in total.")
The output will be:
I have 4 apples and 3 bananas, making 7 pieces of fruit in total.
Kotlin.
val quality = "superhero"
val apples = 4
val bananas = 3
val sentence = "A developer is a $quality. I have ${apples + bananas} fruit"
println(sentence)
The output will be:
A developer is a superhero. I have 7 fruit
Nemerle.
def apples = 4;
def bananas = 3;
Console.WriteLine($"I have $apples apples.");
Console.WriteLine($"I have $(apples + bananas) fruit.");
It also supports advanced formatting features, such as:
def fruit = ["apple", "banana"];
Console.WriteLine($<#I have ..$(fruit; "\n"; f => f + "s")#>);
The output will be:
apples
bananas
Next Generation Shell.
The recommended syntax is codice_13 though codice_14 is also supported:
quality = "superhero"
apples = 4
bananas = 3
sentence = "A developer is a $quality. I have ${apples + bananas} fruit"
echo(sentence)
The output will be:
A developer is a superhero. I have 7 fruit
Nim.
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.
import strutils, strformat
var apples = 4
var bananas = 3
echo "I have $1 apples".format(apples)
echo fmt"I have {apples} apples"
echo fmt"I have {apples + bananas} fruits"
echo fmt"""
I have 
echo fmt"I have {apples=} apples"
echo fmt("I have (apples) {apples}", '(', ')')
echo fmt"""{ "yep\nope" }""" 
The output will be:
I have 4 apples
I have 4 apples
I have 7 fruits
I have
4 apples
I have apples=4 apples
yep
ope
Nix.
let numberOfApples = "4";
in "I have ${numberOfApples} apples"
The output will be:
I have 4 apples
ParaSail.
const Apples := 4
const Bananas := 3
Println ("I have `(Apples) apples.\n")
Println ("I have `(Apples+Bananas) fruit.\n")
The output will be:
I have 4 apples.
I have 7 fruit.
Perl.
my $apples = 4;
my $bananas = 3;
print "I have $apples apples.\n";
print "I have @{[$apples+$bananas]} fruit.\n"; # Uses the Perl array (@) interpolation.
The output will be:
I have 4 apples.
I have 7 fruit.
PHP.
<?php
$apples = 5;
$bananas = 3;
echo "There are $apples apples and $bananas bananas.";
echo "\n";
echo "I have {$apples} apples and {$bananas} bananas.";
The output will be:
There are 5 apples and 3 bananas.
I have 5 apples and 3 bananas.
Python.
apples = 4
bananas = 3
print("I have %d apples and %d bananas" % (apples, bananas)) # no longer recommended
print("I have %(apples)d apples and %(bananas)d bananas" % locals()) # no longer recommended
print("I have {0} apples and {1} bananas".format(apples, bananas))
print("I have {a} apples and {b} bananas".format(b=bananas, a=apples))
print("I have {} apples and {} bananas".format(apples, bananas))
print(f"I have {apples} apples and {bananas} bananas")
The output will be:
I have 4 apples and 3 bananas
Ruby / Crystal.
apples = 4
puts "I have #{apples} apples"
puts "I have %s apples" % apples
The output will be:
I have 4 apples
Rust.
Lacking true interpolated strings, Rust provides a workaround 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, and defining various formatting traits.
let (apples, bananas) = (4, 3);
println!("There are {} apples and {} bananas.", apples, bananas);
The output will be:
 There are 4 apples and 3 bananas.
Scala.
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("I have %d apples\n", apples)
println("I have %d apples" format apples)
//Scala 2.10+
println(s"I have $apples apples")
println(s"I have ${apples + bananas} fruits")
println(f"I have $apples%d apples")
The output will be:I have 4 apples
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(<p>I have {apples} apples</p>);
domElement.$append(<p>I have {apples + bananas} fruits</p>);
Where domElement.$content(<p>I have {apples} apples</p>); gets compiled to this:
domElement.html = "<p>I have " + apples.toHtmlString() + " apples</p>";
Snobol.
 apples = 4 ; bananas = 3
 Output = "I have " apples " apples."
 Output = "I have " (apples + bananas) " fruit."
The output will be:
I have 4 apples.
I have 7 fruit.
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.
let apples = 4
print("I have \(apples) apples")
The output will be:
I have 4 apples
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:
I have 4 apples.
In order to actually format - and not simply replace - the values, there is a formatting function.
set apples 4
puts [format "I have %d apples." $apples]
TypeScript.
As of version 1.4, TypeScript supports string interpolation using backticks codice_12. Here is an example:
var apples: number = 4;
console.log(`I have ${apples} apples`);
The output will be:
I have 4 apples
The codice_16 function can be used as a codice_6 function. The above example can be rewritten, thusly:
var apples: number = 4;
console.log("I have %d apples", apples);
The output remains the same.
Visual Basic.
As of Visual Basic 14, String Interpolation is supported in Visual Basic.
name = "Tom"
Console.WriteLine($"Hello, {name}")
will print "Hello, Tom".