Include an apostrophe in a YAML string
How to put an apostrophe in a YAML string: when plain, single-quoted, double-quoted and block styles work, how to double a single quote, and when a curly apostrophe helps or hurts.
An apostrophe in a YAML value is usually harmless. It causes trouble only when it collides with YAML's own quoting rules. This review covers the ways to write one: leave it unquoted, double it inside single quotes, wrap the value in double quotes, use a block scalar, or swap in a typographic apostrophe. For each, it covers what the change does to the stored text and whether the result stays usable after copying and sharing. The behavior described here follows the YAML specification's general rules. No specific parser, editor or destination was tested for this article, so check your own toolchain before relying on edge cases.
Short answer: which approach fits your use
For most configuration and content files, wrap the value in double quotes and type the apostrophe normally. Double quotes need no special treatment for an apostrophe, and they make the boundaries of the string obvious to the next person editing the file. The tradeoff is that backslash becomes an escape character inside double quotes. Values containing literal backslashes, such as Windows paths or regular expressions, need those backslashes doubled. For those values, a single-quoted string with the apostrophe doubled is often the cleaner choice.
Leaving the value unquoted is acceptable when the apostrophe sits in the middle of ordinary text and the value contains no other YAML-significant sequences. Replacing the straight apostrophe with a curly one avoids quoting conflicts entirely. However, it changes the actual characters stored, so it suits display text better than identifiers, keys or anything that is searched or compared. In those cases, keep the plain straight apostrophe and handle it with quoting instead.
What each method actually changes
Quoting and escaping are notation only. They tell the parser where a string begins and ends. After parsing, the program receives the same text with a single straight apostrophe, whichever valid style you used. Swapping in a typographic apostrophe is different. It replaces one Unicode character, the straight apostrophe U+0027, with another, the right single quotation mark U+2019. The loaded value then contains a different character, not just a different presentation of the same one.
| Style | How the apostrophe is written | Loaded value | Main caveat |
| Plain (unquoted) | It's ready | It's ready | Cannot begin with an apostrophe; breaks on sequences like a colon followed by a space or a space followed by # |
| Single-quoted | 'It''s ready' | It's ready | Each apostrophe must be doubled; backslash has no special meaning |
| Double-quoted | "It's ready" | It's ready | Backslash starts an escape, so literal backslashes must be doubled |
| Block scalar (| or >) | It's ready, on an indented line below the key | It's ready plus a line ending, unless a chomping indicator such as |- is used | Indentation-sensitive; adds a trailing line break by default |
| Typographic apostrophe | It’s ready (U+2019), or "It\u2019s ready" in double quotes | It’s ready, a different character from It's ready | Changes stored text; affects search, matching and comparison |
Unquoted values
A plain scalar may contain an apostrophe anywhere except as its first character. A leading apostrophe tells the parser that a single-quoted string is starting. A value like a contraction or possessive in the middle of a sentence loads correctly without quotes. Plain scalars have other restrictions that are unrelated to apostrophes, notably a colon followed by a space and a space followed by a hash sign. Quoting removes those risks at the same time.
title: It's ready
owner: The team's settings
Single-quoted values: double the apostrophe
Inside single quotes, the only escape YAML recognizes is a doubled single quote, which stands for one apostrophe. Backslash sequences are not interpreted, so a backslash is stored literally. This is the style to use when a value begins with an apostrophe, as in a place name or abbreviation, or when it also contains backslashes.
title: 'It''s ready'
place: '''s-Hertogenbosch'
pattern: 'C:\Users\It''s here'
In the second line, the outer quotes delimit the string and the doubled pair produces the leading apostrophe. The loaded value is 's-Hertogenbosch. In the third line, the backslashes are kept exactly as typed.
Double-quoted values: no escape needed
An apostrophe needs no escaping inside double quotes. A common mistake is writing a backslash before it, borrowed from other languages. A backslash followed by an apostrophe is not among the escape sequences the YAML specification defines, so a conforming parser should reject it. Some parsers may be more lenient, but you should not depend on that. Double quotes do require escaping a literal double quote and a literal backslash.
title: "It's ready"
quote: "She said \"it's fine\""
wrong: "It\'s ready" # not a valid YAML escape
Block scalars for longer text
For multi-line prose, a literal (|) or folded (>) block scalar avoids quoting entirely, because no characters act as delimiters inside the indented block. By default, a block scalar keeps one trailing line break. Adding a minus sign after the indicator, as in |-, removes it. This matters when the value is compared or inserted inline.
description: |-
It's ready.
The team's notes are attached.
Typographic apostrophe substitution
Typing the curly apostrophe directly, or writing the escape \u2019 inside double quotes, sidesteps all quoting rules. YAML treats only the straight single and double quote marks as delimiters. Many style guides prefer the curly form for published prose. The cost is that the value no longer matches text typed with an ordinary keyboard apostrophe. The \u2019 escape works only in double-quoted strings. In single-quoted or plain values, it is stored as the literal six characters.
heading: It’s ready
heading_escaped: "It\u2019s ready"
Will the result stay readable and usable after copying or sharing?
Quoted and escaped forms only matter while the text is still YAML. Once a program loads the file, the extra quotes and doubled apostrophes are gone. If someone copies the raw line from the file, they get the notation too. For example, pasting 'It''s ready' into a non-YAML context yields the doubled apostrophe and the surrounding quotes. When the text is meant to be read or reused outside the file, share the loaded value or a plain-text version, not the source line.
- Smart-quote conversion: word processors, chat apps and some note tools may automatically turn typed straight quotes into curly ones. If a delimiting quote is converted, YAML no longer recognizes it as a delimiter. The value then loads as a plain scalar with the curly marks included, or the file fails to parse. Edit YAML in a plain-text or code editor, and be cautious when pasting snippets from formatted documents.
- Search and matching: a curly apostrophe and a straight one are different characters. Searching, filtering, deduplicating or comparing values may treat 'it's' and 'it’s' as different unless the software normalizes them. This was not tested for any specific tool. Keep identifiers, keys, file names and lookup values on the straight apostrophe.
- Encoding: a file with curly apostrophes must be saved in a Unicode encoding. YAML 1.2 expects UTF-8, UTF-16 or UTF-32. A file re-saved in a legacy encoding along the way may show garbled characters in place of the apostrophe.
- Other layers: if the YAML value is later passed through a shell, a templating engine or another serialization format, that layer may apply its own quoting rules to the apostrophe. Valid YAML does not guarantee the value is safe downstream, so check each layer it passes through.
- Assistive technology: screen readers generally receive the loaded text rather than the YAML notation. No documentation or test was supplied showing how specific screen readers announce a straight versus a curly apostrophe. Treat any claimed difference as unverified until you test it in the environment you care about.
When to keep plain text
Keep the ordinary straight apostrophe, handled with double quotes or a doubled single quote, whenever the value is used by software rather than read by people. That includes keys, IDs, tags, file names, URLs, anything matched against user input, and anything that must round-trip through other tools unchanged. Reserve the curly apostrophe for display copy where typography matters and you control how the text is searched and rendered. Whichever form you choose, use it consistently within a file, because mixed apostrophes are hard to spot and cause confusing mismatches.
Known gaps in this review
The rules above reflect the YAML specification's general quoting behavior, not observations from a particular parser, editor or platform. Parser leniency toward invalid escapes and how specific applications treat curly apostrophes in search were not verified. The same goes for how particular screen readers announce either character. Load a sample file with the parser you actually use and check the resulting value before committing to an approach.