Lably
Skip to content

String literal

Delimited series of characters that represent a string in code

  • 25 min read
  • 8 chapters

Related on Lably

Through String (computer science), Character (computing) and Delimiter

Syntax

Bracket delimited

A bracketed string literal is delimited by a start and an end character. The language can specify the use of any characters as delimiters.

Quotation is the most common way to delimit a string literal. Many languages support double-quotes (i.e. "Hello") and/or single-quotes (i.e. 'there'). When both are supported, delimiter collision can be minimized by treating one style of quotes as normal text when enclosed in quotes of the other style. In Python the literal "Dwayne 'the rock' Johnson" is valid since the outer quotes are double, making the inner single quotes regular text.

An empty string is written as "" or ''.

Paired delimiters are two different types of characters where one is used at the beginning of a literal and the other used at the end. With paired delimiters, the language can support embedding quotes in the literal text – as long as they all are paired and don't partially jump out of their own scope. For example, PostScript uses parentheses, as in (The quick (brown fox)) and m4, uses backtick ` at the start, and apostrophe ' at the end. Tcl allows both quotes and braces, as in "The quick brown fox" or {The quick {brown fox}}; this derives from the single quotations in Unix shells and the use of braces in C for compound statements, since blocks of code is in Tcl syntactically the same thing as string literals – that the delimiters are paired is essential for making this feasible.

Quotation is most commonly via unpaired quotes, but some tools and character sets support paired quotes. Unpaired quotes are quotes that don't have unique left-side-quote and right-side-quote variants, including "" or ''. The Unicode character set includes paired versions:

“Hi there!” ‘Hi there!’ „Hi there!“ «Hi there!»

Scope Mixing

Scope mixing is where scopes are mixed together instead of being purely hierarchical.

In other words, "scope mixing" is where an unambiguous beginning delimiter (or an unambiguous ending delimiter) doesn't exist.

Examples of a quote jumping out of its own scope (which are illegal strings) would be:

  • "InsideTheOuterString 'InnerString_ButWithUnmatchedEndingSingleQuoteThatRendersThisStringInvalid" ErroneousStringThatIsSomehowInsideTheInnerStringYetNotInsideTheOuterString'
  • "1 '2" 3'
  • "Inside1 'InsideBoth1and2" Inside2'

String literals do not allow scope mixing, as it creates ambiguous programs. A string literal may sometimes appear to have mixed scopes, but there are operator-precedence rules made for each specific programming language to prevent ambiguity, such as the tactic of reading expressions from the leftmost character to the rightmost character, giving a left " symbol a higher precedence (i.e., priority) over any other quotation mark that comes after it (on the same line).

As mentioned earlier, nested quotes can be valid, such as in "Dwayne 'the rock' Johnson", but they require some method that employs a hierarchy (typically a stack data structure), regardless of whether that hierarchy uses unique delimiting characters, escape characters, or repeated-character delimiters.

"Scope-mixing" is a specific case of Delimiter Collision, explained later in this page.

Whitespace delimited

A language might support multi-line strings. In YAML, string literals may be specified by the relative positioning of whitespace and indentation.

<span id="mwWw"></span><span class="w" id="mwXA"> </span><span class="p p-Indicator" id="mwXQ">-</span><span class="w" id="mwXg"> </span><span class="nt" id="mwXw">title</span><span class="p" id="mwYA">:</span><span class="w" id="mwYQ"> </span><span class="l l-Scalar l-Scalar-Plain" id="mwYg">An example multi-line string in YAML</span> <span class="w" id="mwYw"> </span><span class="nt" id="mwZA">body </span><span class="p" id="mwZQ">:</span><span class="w" id="mwZg"> </span><span class="p p-Indicator" id="mwZw">|</span> <span class="w" id="mwaA"> </span><span class="no" id="mwaQ">This is a multi-line string.</span> <span class="w" id="mwag"> </span><span class="no" id="mwaw">"special" metacharacters may</span> <span class="w" id="mwbA"> </span><span class="no" id="mwbQ">appear here. The extent of this string is</span> <span class="w" id="mwbg"> </span><span class="no" id="mwbw">represented by indentation.</span>

Word delimited

Some languages, such as Perl and PHP, allow string literals that are delimited the same as words in a natural language. In the following Perl code, for example, red, green, and blue are string literals, even though not quoted:

<span id="mweA"></span><span class="nv" id="mweQ">%map</span><span class="w" id="mweg"> </span><span class="o" id="mwew">=</span><span class="w" id="mwfA"> </span><span class="p" id="mwfQ">(</span><span class="n" id="mwfg">red</span><span class="w" id="mwfw"> </span><span class="o" id="mwgA">=></span><span class="w" id="mwgQ"> </span><span class="mh" id="mwgg">0x00f</span><span class="p" id="mwgw">,</span><span class="w" id="mwhA"> </span><span class="n" id="mwhQ">blue</span><span class="w" id="mwhg"> </span><span class="o" id="mwhw">=></span><span class="w" id="mwiA"> </span><span class="mh" id="mwiQ">0x0f0</span><span class="p" id="mwig">,</span><span class="w" id="mwiw"> </span><span class="n" id="mwjA">green</span><span class="w" id="mwjQ"> </span><span class="o" id="mwjg">=></span><span class="w" id="mwjw"> </span><span class="mh" id="mwkA">0xf00</span><span class="p" id="mwkQ">);</span>

Perl treats a non-reserved sequence of alphanumeric characters as string literal in most contexts. For example, the following two lines of Perl are equivalent:

<span id="mwlQ"></span><span class="nv" id="mwlg">$y</span><span class="w" id="mwlw"> </span><span class="o" id="mwmA">=</span><span class="w" id="mwmQ"> </span><span class="s" id="mwmg">"x"</span><span class="p" id="mwmw">;</span> <span class="nv" id="mwnA">$y</span><span class="w" id="mwnQ"> </span><span class="o" id="mwng">=</span><span class="w" id="mwnw"> </span><span class="n" id="mwoA">x</span><span class="p" id="mwoQ">;</span>

Declarative notation

The length of a literal can be encoded into the beginning of the text which alleviates the need for marking the beginning and end of a string. For example, in FORTRAN, string literals were written in Hollerith notation, where a decimal count of the number of characters was followed by the letter H, and then the characters of the string.

<span id="mwqA"></span><span class="mi" id="mwqQ">35</span><span class="n" id="mwqg">HAn</span><span class="w" id="mwqw"> </span><span class="n" id="mwrA">example</span><span class="w" id="mwrQ"> </span><span class="n" id="mwrg">Hollerith</span><span class="w" id="mwrw"> </span><span class="n" id="mwsA">string</span><span class="w" id="mwsQ"> </span><span class="n" id="mwsg">literal</span>

A drawback of this technique is that it is relatively error-prone unless length insertion is automated, especially for multi-byte encodings. Advantages include: alleviates need to search for the end delimiter and therefore requires less computational overhead, prevents delimiter collision issues and enables the inclusion of metacharacters that might otherwise be mistaken as commands

Sign in to keep reading

7 more chapters are free with a Lably account.

More in Computing and communication