Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
String literal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== String interpolation == {{main|String interpolation}} In some languages, string literals may contain placeholders referring to variables or expressions in the current [[Scope (computer science)|context]], which are evaluated (usually at run time). This is referred to as ''variable interpolation'', or more generally [[string interpolation]]. Languages that support interpolation generally distinguish strings literals that are interpolated from ones that are not. For example, in [[bourne shell|sh-compatible Unix shells]] (as well as Perl and Ruby), double-quoted (quotation-delimited, ") strings are interpolated, while single-quoted (apostrophe-delimited, <nowiki>'</nowiki>) strings are not. Non-interpolated string literals are sometimes referred to as "raw strings", but this is distinct from "raw string" in the sense of escaping. For example, in Python, a string prefixed with <code>r</code> or <code>R</code> has no escaping or interpolation, a normal string (no prefix) has escaping but no interpolation, and a string prefixed with <code>f</code> or <code>F</code> has escaping and interpolation. For example, the following [[Perl]] code: <syntaxhighlight lang="perl"> $name = "Nancy"; $greeting = "Hello World"; print "$name said $greeting to the crowd of people."; </syntaxhighlight> produces the output: Nancy said Hello World to the crowd of people. In this case, the [[metacharacter]] character ($) (not to be confused with the [[Sigil (computer programming)|sigil]] in the variable assignment statement) is interpreted to indicate variable interpolation, and requires some escaping if it needs to be outputted literally. This should be contrasted with the <code>[[printf]]</code> function, which produces the same output using notation such as: <syntaxhighlight lang="perl">printf "%s said %s to the crowd of people.", $name, $greeting;</syntaxhighlight> but does not perform interpolation: the <code>%s</code> is a placeholder in a [[printf format string]], but the variables themselves are outside the string. This is contrasted with "raw" strings: <syntaxhighlight lang="perl">print '$name said $greeting to the crowd of people.';</syntaxhighlight> which produce output like: $name said $greeting to the crowd of people. Here the $ characters are not metacharacters, and are not interpreted to have any meaning other than plain text.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)