Quotation marks in computer code
A style guide
There are five common types of quotation marks or “quotes” in code by English-language programmers. This article argues for limiting the use of ASCII quotation marks to places where it is syntactically relevant.
Characters in red in the following table are not recommended for any use outside of linguistics.
|Accent marks||´`||˝ ̏|
The ’ character, a closing single directed quotation mark, is also used as an apostrophe.
Accent marks are not quotation marks. In particular, ´, ˝ and ̏ do not show that something is quoted.
The single backtick (`) is an accent mark but also serves a purpose analogous to that of a quotation mark, not in English but in some programming languages, including ECMAScript and Linux shells. There are historical reasons for this in the limits of pre-Unicode character sets.
Reserve undirected (ASCII) quotation marks and backticks for their syntactically significant uses in computer code, i.e. for the needs of machines.
Where it makes no difference to syntax, prefer single undirected quotation marks to enclose string literals.
For all other purposes where your environment permits it, including comments in code, use typographical quotation marks.
This is a good string literal in both Python and ECMAScript:
reminder = '“I won’t”, I said.'
When undirected quotation marks are only used for code, they help identify code. When directed quotation marks are used for everything else, you don’t risk any confusion or collision with your programming language. This is an example of the proper separation of concerns.
Typographical quotation marks, because they are directed, are inherently less ambiguous than ASCII, and therefore easier to nest.
Typographical quotation marks are easiest for humans to read. At the same time, single undirected quotation marks cause less clutter than doubles.
'' is visually less busy than
"", and therefore distract less from content.
When there is no longer any need to escape or vary quotation marks to include a quotation or apostrophe inside a string literal, writing becomes easier and text is more amenable to be searched. This helps in common tasks, like tracking down error messages and refactoring.
Unicode is widely available. If you can’t use it, this recommendation does not apply.
How to write quotation marks
On a typical Linux desktop system with a QWERTY keyboard layout, directed quotation marks can be produced with
Shift+AltGr+v for ‘ (open quote) and
Shift+AltGr+b for ’ (close quote, apostrophe), in any text editor.
On a Colemak layout, it’s
Details and examples by language
Some languages distinguish syntactically between ' and ". In such languages, syntax takes priority over style, but this has no effect on the appropriateness of typographical quotation marks.
Except where interdicted by official stylistic standards like PEP 257 or local practical concerns, use single undirected quoation marks for string literals.
These examples of Python are all bad:
""I won't," I said." # Syntactically invalid.
'"I won't," I said.' # Syntactically invalid.
'"I won\'t," I said.' # Awkward to read and grep for.
'''"I won't," I said.''' # Awkward to read, long.
"``I won't,'' I said." # Not quotation marks, long.
"´´I won`t,`` I said." # Not quotation marks, long.
'˝I won`t, ̏ I said.' # Not quotation marks.
"“I won’t,” I said." # Mildly cluttered.
If you use Black, configure it to
skip-string-normalization. If you don’t take that step, Black creates visual clutter by always doubling quotation marks, because of a misreading of PEP 257. Consider Autopep8 for more gentle, standards-compliant automatic formatting.
Use backticks to evaluate variables inside strings. Prefer ' over " to delimit all other strings.
Bash and other common shells
' prevents variable evaluation. " does not. Use them accordingly. Backticks have a special technical function in shell scripting, but please prefer modern
$(subshell) notation over backticks.
Languages without room for style
The following languages offer no room for stylistic choices of quotation marks in language syntax, which is great. Use what they require, and use typographical quotation marks.
' is short for the
quote special form. Only " delimits strings. ` does “syntax quoting” for macros.
" delimits normal string literals and ' both character literals (enclosing) and lifetime annotations (leading only). If you try to use backticks, the Rust compiler will truthfully tell you that “Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not”.
" delimits normal string literals, ` “raw” strings, and ' “rune” literals.