<p>In most modern languages, string literals can use either single (<code>'</code>) or double (<code>&quot;</code>) quotes. Are there any reasons to distinguish between the two, and if so what would the other kind be used for?</p>

<syntax><language-design><strings>
Should my language distinguish between single- and double-quoted literals?
CC BY-SA 4.0
2023-05-18T01:19:50.487Z
"In most modern languages, string literals can use either single (') or double (") quotes." This requires both citations and definitions.
Karl Knechtel
CC BY-SA 4.0
2023-05-17T15:31:59.557Z
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.
rydwolf
<h1>Embedded variables/escapes</h1>
<p>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 <code>'\r'</code> can be used as a literal for <code>\r</code> instead of <code>&quot;\\r&quot;</code>, and similar, preventing the headache-inducing nested backslash escaping you can run into.</p>
<p>Additionally, some languages allow things like embedding variables/expressions directly, and single quoted strings could disable this.</p>

CC BY-SA 4.0
2023-05-17T15:34:01.747Z
2023-05-17T15:34:01.747Z
rydwolf
CC BY-SA 4.0
2023-06-01T19:56:50.720Z
@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]==]`
Jasmijn
CC BY-SA 4.0
2023-05-17T15:58:21.200Z
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.
Silvio Mayolo
CC BY-SA 4.0
2023-05-17T16:05:17.147Z
@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.
rydwolf
<p>Firstly, having more than one is always useful when you need to have quotes within quotes.</p>
<p>But beyond that it depends on whether you need any semantic distinction:</p>
<ul>
<li>C-like languages use single quotes to delimit <code>char</code> values, while double quotes delimit <code>array of char</code> values. (In languages like C, the distinction between array and element types is important. Furthermore, C does not have a &quot;string&quot; data-type; it simply stores strings in array-of-char.)</li>
<li>Shell-like languages use double quotes around strings that may have various kinds of interpolations done to them; <code>&quot;$var&quot;</code> will expand <code>var</code> 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.)</li>
<li>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.)</li>
<li>Python uses single quotes, double quotes, and triple-double quotes. The latter <code>&quot;&quot;&quot;</code> is used around multi-line strings.</li>
</ul>
<p>I find having <em>only</em> two kinds of quotes seems rather limiting. In addition to the ASCII apostrophe <code>'</code> and quotation mark <code>&quot;</code>, I routinely use <code>« guillemots »</code> (french quotes), typographic <code>‘single quotes’</code> and <code>“double quotes”</code>, and the distinct symbols for degrees <code>°</code> minutes/feet <code>′</code> and seconds/inches <code>″</code> – and where necessary, thirds <code>‴</code>.</p>

CC BY-SA 4.0
2024-01-25T16:04:32.117Z
2024-01-25T16:04:32.117Z
Martin Kealey
<h1>Avoid escaping nested quotes</h1>
<p>This is what I do when programming in JavaScript:</p>
<pre><code>let foo = &quot;This doesn't need to be escaped&quot;;
let bar = 'Neither does &quot;this&quot;';
</code></pre>
<p>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.</p>

CC BY-SA 4.0
2023-05-18T00:29:08.097Z
2023-05-18T00:29:08.097Z
Bbrk24
CC BY-SA 4.0
2023-05-18T01:23:16.273Z
This is a large part of the motivation in Python, as I understand it.
Karl Knechtel
<h1>It's a waste of a character to have both the same</h1>
<p>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.</p>
<p>One plausible use for the apostrophe is as an <strong>identifier character</strong>: 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.</p>
<p>It could be used for marking <strong>generic type parameters</strong>, as in ML-family languages:</p>
<pre><code>let prepend (ls : 'a list) (value : 'a) = value :: ls
</code></pre>
<p>Another use is for <strong>other literal types</strong>. It's been used as a prefix for symbol literals <code>'a</code> 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.</p>
<p>It could <strong>inhibit evaluation</strong> of a compound value, again like in Lisp:</p>
<pre><code>'(print x)
</code></pre>
<p>It could be another scarce <strong>operator character</strong> to use: I don't know what <code>a ' b</code> should do, but the users of the language will figure it out.</p>
<p>All of these could well be more effective uses than a redundant way to write a string.</p>

CC BY-SA 4.0
2023-05-18T00:05:47.427Z
2023-05-18T00:05:47.427Z
Michael Homer
CC BY-SA 4.0
2023-12-11T16:22:00.537Z
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.
supercat
CC BY-SA 4.0
2024-01-26T16:12:02.287Z
Rust uses `'a` for "the lifetime a" in lifetime parameters.
Pablo H
CC BY-SA 4.0
2023-05-24T07:40:12.693Z
Similar to what I was thinking of: “ code golf”
The Empty String Photographer
<h1>Character Literals</h1>
<p>Rust, and several other languages, use single-quoted &quot;strings&quot; for literal character types.</p>

CC BY-SA 4.0
2023-05-17T15:30:57.010Z
2023-05-17T15:30:57.010Z
Ginger
CC BY-SA 4.0
2024-01-31T12:50:26.097Z
@KarlKnechtel I know that, hence my quoting of "string".
Ginger
CC BY-SA 4.0
2023-05-18T01:20:42.277Z
Character literals **are not** a kind of string literal.
Karl Knechtel
# Should my language distinguish between single- and double-quoted literals?
Posted by: username_0 on 2023-05-17
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?
**Tags:** syntax language-design strings
> **Comments:**
> **username_2**: 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.  
> **username_1**: "In most modern languages, string literals can use either single (') or double (") quotes." This requires both citations and definitions.  
---
## 5 answers
### Answer by username_0 on 2023-05-17. Score: 16
# Character Literals

Rust, and several other languages, use single-quoted "strings" for literal character types.
> **Comments:**
> **username_1** (2023-05-18): Character literals **are not** a kind of string literal.  
> **username_0** (2024-01-31): @KarlKnechtel I know that, hence my quoting of "string".  
---
### Answer by username_6 on 2023-05-18. Score: 12
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.
> **Comments:**
> **username_1** (2023-05-18): This is a large part of the motivation in Python, as I understand it.  
---
### Answer by username_7 on 2023-05-18. Score: 9
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.
> **Comments:**
> **username_10** (2023-05-24): Similar to what I was thinking of: “ code golf”  
> **username_8** (2023-12-11): 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.  
> **username_9** (2024-01-26): Rust uses `'a` for "the lifetime a" in lifetime parameters.  
---
### Answer by username_2 on 2023-05-17. Score: 8
# 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.
> **Comments:**
> **username_4** (2023-05-17): 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.  
> **username_2** (2023-05-17): @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.  
> **username_3** (2023-06-01): @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]==]`  
---
### Answer by username_5 on 2024-01-25. Score: 0
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 delimit `array 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 expand `var` 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 `‴`.
---
