Content deleted Content added
Subcelestial (talk | contribs) |
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>"[
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|
=== Groovy ===
Line 151:
</syntaxhighlight>
=== 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">
print(f'I have {
</syntaxhighlight>
The output will be:
Line 429:
<syntaxhighlight lang="output">I have 4 apples.</syntaxhighlight>
In order to actually format
<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>
|