In most modern languages, string literals can use either single ('
) or double ("
) quotes. Are there any reasons to distinguish between the two, and if so what would the other kind be used for?
-
1$\begingroup$ Gonna pre-emptively comment this: I think "should" in this title is fine, and doesn't make it any more subjective than "pros and cons" or "advantages" would. $\endgroup$– rydwolfCommented May 17, 2023 at 15:31
-
1$\begingroup$ "In most modern languages, string literals can use either single (') or double (") quotes." This requires both citations and definitions. $\endgroup$– Karl KnechtelCommented May 18, 2023 at 1:19
5 Answers
Character Literals
Rust, and several other languages, use single-quoted "strings" for literal character types.
-
4$\begingroup$ Character literals are not a kind of string literal. $\endgroup$ Commented May 18, 2023 at 1:20
-
$\begingroup$ @KarlKnechtel I know that, hence my quoting of "string". $\endgroup$– GingerCommented Jan 31 at 12:50
Avoid escaping nested quotes
This is what I do when programming in JavaScript:
let foo = "This doesn't need to be escaped";
let bar = 'Neither does "this"';
Is it a deal-breaker to not have this? Absolutely not! But it is something to consider, and it is (ab)used by many minifiers.
-
3$\begingroup$ This is a large part of the motivation in Python, as I understand it. $\endgroup$ Commented May 18, 2023 at 1:23
It's a waste of a character to have both the same
Having two semantically identical quotation mechanisms sitting on two keyboard characters may not be worth the cost. If there isn't a substantially different meaning, one of them could be used for something else instead, and that something doesn't have to be paired around a string! Several existing languages already make use of the single quote for other purposes.
One plausible use for the apostrophe is as an identifier character: it's common in mathematics to name derivative variables with a prime x, x′, x′′, and the single quote approximates that well. This could be particularly useful for a language that prohibited shadowing of names. Haskell allows this.
It could be used for marking generic type parameters, as in ML-family languages:
let prepend (ls : 'a list) (value : 'a) = value :: ls
Another use is for other literal types. It's been used as a prefix for symbol literals 'a
in Lisp and other languages, and the language may have other atomic values it wants to express. Character literals are a common use in C-like languages.
It could inhibit evaluation of a compound value, again like in Lisp:
'(print x)
It could be another scarce operator character to use: I don't know what a ' b
should do, but the users of the language will figure it out.
All of these could well be more effective uses than a redundant way to write a string.
-
$\begingroup$ Similar to what I was thinking of: “ code golf” $\endgroup$ Commented May 24, 2023 at 7:40
-
$\begingroup$ Being able to write strings that contain quote marks as e.g.
console.log('This string contains "quote marks"!');
can be handy, even if apostrophe-delimited and quote-delimited strings would otherwise be interchangeable. $\endgroup$– supercatCommented Dec 11, 2023 at 16:22 -
$\begingroup$ Rust uses
'a
for "the lifetime a" in lifetime parameters. $\endgroup$– Pablo HCommented Jan 26 at 16:12
Embedded variables/escapes
Some languages, especially shell languages, use single quoted literals as raw literals, where things like backslash escapes are disabled. This can be handy when doing things like implementing encoders and decoders for JSON, or language parsers, since '\r'
can be used as a literal for \r
instead of "\\r"
, and similar, preventing the headache-inducing nested backslash escaping you can run into.
Additionally, some languages allow things like embedding variables/expressions directly, and single quoted strings could disable this.
-
$\begingroup$ I'm personally a fan of interpolation-by-default, and then allowing you to turn off interpolation and escaping with a prefix like
raw"..."
. But using single quotes to disable these things is pretty common in the state of the art. $\endgroup$ Commented May 17, 2023 at 15:58 -
5$\begingroup$ @SilvioMayolo One thing Rust does that I really like is that the
r
(raw) prefix can have#
s after it, and it can only be ended by a quote followed by that many#
s. So you can put arbitrary data in a raw string without escaping, just by increasing the number of#
s until there's no ambiguity. $\endgroup$– rydwolfCommented May 17, 2023 at 16:05 -
$\begingroup$ @RydwolfPrograms Lua has something similar: string literals can be written as
[[use ' and " without escaping]]
, and sequences of matching length of=
can be included like so:[==[now we can use [[, ]], [=[ and ]=] inside a string literal without escaping as well]==]
$\endgroup$– JasmijnCommented Jun 1, 2023 at 19:56
Firstly, having more than one is always useful when you need to have quotes within quotes.
But beyond that it depends on whether you need any semantic distinction:
- C-like languages use single quotes to delimit
char
values, while double quotes delimitarray of char
values. (In languages like C, the distinction between array and element types is important. Furthermore, C does not have a "string" data-type; it simply stores strings in array-of-char.) - Shell-like languages use double quotes around strings that may have various kinds of interpolations done to them;
"$var"
will expandvar
but then inhibit any further processing. Compare this with single quotes, which cause everything to be taken literally, and no quotes, meaning that values not only have interpolations, but then also are subject to word-splitting and globbing. (Having things this way around is a major source of bugs in shell scripts.) - SQL uses single quotes to indicate fixed string values, and double quotes to write object names that aren't simply alphanumeric. (Some dialects also use backticks for this.)
- Python uses single quotes, double quotes, and triple-double quotes. The latter
"""
is used around multi-line strings.
I find having only two kinds of quotes seems rather limiting. In addition to the ASCII apostrophe '
and quotation mark "
, I routinely use « guillemots »
(french quotes), typographic ‘single quotes’
and “double quotes”
, and the distinct symbols for degrees °
minutes/feet ′
and seconds/inches ″
– and where necessary, thirds ‴
.