YAML by Example: Quoted vs Unquoted Strings
1.2
When to use quotes. Learn string escaping and special cases.
Code
# Unquoted strings (most common)
name: John Smith
message: Hello World
# Single quotes (literal, no escaping)
path: 'C:\Users\Admin'
quote: 'He said "hello"'
# Double quotes (allows escape sequences)
escaped: "Line 1\nLine 2\nLine 3"
special: "Tab:\tNew section"
# Strings that look like other types need quotes
zip_code: "12345" # Without quotes, this becomes a number
yes_string: "yes" # Without quotes, this becomes boolean true
version: "1.2" # Without quotes, this becomes a float
# Special characters require quotes
colon_value: "http://example.com"
hash_tag: "#trending"Explanation
Unquoted strings work for simple text without special characters. Single quotes preserve everything literally, so backslashes and double quotes don't need escaping. Double quotes allow escape sequences like newlines or tabs, making them useful for formatted text.
Quote strings that could be misinterpreted as numbers, booleans, or null values. Also quote strings containing YAML special characters like colons, hashes, or brackets. When in doubt, using quotes makes your intent explicit and prevents parsing surprises.
Code Breakdown
6
Single quotes treat backslashes as literal characters.
10
Double quotes process escape sequences like
\n for newlines.
