Guide

Quotes in YAML: single, double, or unquoted strings

Learn when to use single quotes, double quotes, or plain scalars in YAML and how they affect escapes, comments, and types.

by Tools in a Tab · Published on · Reviewed on

Short answer

In YAML, unquoted text is convenient, single quotes preserve almost everything literally, and double quotes interpret escapes such as \n, \t, or \u263A. Quote a value when it could be mistaken for syntax or another type; choose double quotes only when escape sequences are useful.

Three styles with different behavior

unquoted: api-service
single: 'C:\temp\notes.txt'
double: "first line\nsecond line"

The first value is a plain scalar. In the second, a backslash does not begin an escape. In the third, \n becomes a newline in the resulting value. Paste this example into the YAML to JSON converter to inspect the exact types and content instead of judging only the source text.

When a value should be quoted

Quotes are useful when text:

  • starts with an indicator such as -, ?, &, *, !, or #;
  • contains : and could look like a key/value pair;
  • contains # and the remainder could become a comment;
  • matches a Boolean, number, or null value under the active schema;
  • must preserve leading or trailing spaces;
  • must unambiguously remain a string when converted to another format.

For example, code: 00123 may be resolved as a number by some schema and processor. code: '00123' explicitly states that the zeros belong to the text.

Single-quoted scalars

Inside single quotes, represent one single quote by doubling it:

message: "Ada said: 'ready'."
path: 'C:\temp\notes.txt'

Backslashes and double quotes remain literal. This style suits paths, regular expressions, and text containing many backslashes. It does not provide backslash escape sequences for control characters.

Double-quoted scalars

Double quotes interpret the escape sequences defined by YAML:

message: "one\ntwo"
tabbed: "key\tvalue"
unicode: "smile: \u263A"

Write \\ for a literal backslash. Copying a path from a single-quoted scalar to a double-quoted one without reviewing the slashes can change its content or produce an invalid escape.

Frequent mistakes

  • Expecting \n to create a newline inside single quotes.
  • Writing # after a space in an unquoted scalar and losing the rest as a comment.
  • Quoting only some ambiguous values without a consistent policy.
  • Confusing the visual style of a scalar with its parsed value.
  • Adding quotes to fix a problem that is actually caused by indentation.

The YAML 1.2.2 specification distinguishes plain, single-quoted, and double-quoted scalars. Backslash escapes are interpreted only by the double-quoted style. Choose the form that makes the value received by the application explicit.