String interpolation: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Add: title, date. Changed bare reference to CS1/2. | Use this bot. Report bugs. | Suggested by Folkezoft | Linked from User:Folkezoft/sandbox | #UCB_webform_linked 829/978
 
(8 intermediate revisions by 7 users not shown)
Line 1:
{{short description|Replacing placeholders in a string with values}}
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 [[Form (document)#Placeholders|placeholders]], yielding a result in which the placeholders are replaced with their corresponding values. It is a form of simple [[Template processor|template processing]]<ref>"[httphttps://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf Enforcing Strict Model-View Separation in Template Engines]", T. Parr (2004), WWW2004 conference.</ref> or, in formal terms, a form of [[quasi-quotation]] (or logic [[Substitution (logic)|substitution]] interpretation). The placeholder may be a variable name, or in some languages an arbitrary expression, in either case evaluated in the current [[Scope (computer science)|context]].
 
String interpolation is an alternative to building string via [[concatenation]], which requires repeat quoting and unquoting;<ref>{{Cite web|url=http://perlmeme.org/howtos/using_perl/interpolation.html|title = Interpolation in Perl |date = 12 December 2024 |quote="This is much tidier than repeat uses of the '.' concatenation operator."}}</ref> or substituting into a [[printf format string]], where the variable is far from where it is used. Compare:
<syntaxhighlight lang="ruby">
apples = 4
Line 112:
=== Go ===
{{Main|Go (programming language)}}
{{as of|20222025}}, Go does not have string interpolation. There have been some proposals for string interpolation, inwhich thehave nextbeen version of the language, Go 2rejected.<ref>{{cite web|url=https://github.com/golang/go/issues/34174 |title=proposal: Go 2: string interpolation #34174|website=[[GitHub]] }}</ref><ref>{{cite web|url=https://github.com/golang/go/issues/50554 |title=proposal: Go 2: string interpolation evaluating to string and list of expressions #50554|website=[[GitHub]] }}</ref><ref>{{Cite Instead,web Go|title=proposal: usesspec: [[printfadd formatsimple string]]s ininterpolation thesimilar <code>fmt.Sprintf</code>to function,Swift string· [[concatenation]],Issue or#57616 template· librariesgolang/go like|url=https://github.com/golang/go/issues/57616 |access-date=2025-05-19 |website=GitHub |language=en}}<code>text/template</coderef>.
 
=== Groovy ===
Line 151:
</syntaxhighlight>
 
ItThey waswere removed in Java 23 due to security and design issues.<ref>{{cite web | title=Significant Changes in the JDK | url=https://docs.oracle.com/en/java/javase/23/migrate/significant-changes-jdk-release.html }}</ref>
 
=== JavaScript ===
Line 314:
{{Main|Python (programming language)}}
Python supports string interpolation as of version 3.6, referred to as
"formatted string literals" or "f-strings".<ref>{{cite web |url=https://docs.python.org/3/tutorial/inputoutput.html#tut-f-strings
|title= The Python Tutorial: 7.1.1. Formatted String Literals}}</ref><ref>{{Cite web |url=https://docs.python.org/3/reference/lexical_analysis.html#f-strings |title=The Python Language Reference: 2.4.3. Formatted string literals}}</ref><ref>{{Cite web|url=https://www.python.org/dev/peps/pep-0498/|title = PEP 498 -- Literal String Interpolation}}</ref> Such a literal begins with an <code>f</code> or <code>F</code> before the opening quote, and uses braces for placeholders:
<syntaxhighlight lang="python">
applesnum_apples = 4
bananasnum_bananas = 3
print(f'I have {applesnum_apples} apples and {bananasnum_bananas} bananas')
</syntaxhighlight>
The output will be:
Line 429:
<syntaxhighlight lang="output">I have 4 apples.</syntaxhighlight>
 
In order to actually format - and not simply replace - the values, there is a formatting function.
 
<syntaxhighlight lang="tcl">
Line 453:
The output remains the same.
 
=== Visual Basic .NET ===
As of Visual Basic 14, string interpolation is supported in Visual Basic.<ref>{{Cite web|last=KathleenDollard|title=Interpolated Strings - Visual Basic|url=https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/strings/interpolated-strings|access-date=2021-06-20|website=docs.microsoft.com|language=en-us}}</ref>