Skip to main content

How to Write a String in YAML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Most YAML text needs no quotes: name: Bike shop is already a string. But some words and numbers mean something else to YAML, so yes becomes true and 3.10 becomes a number. This guide writes strings three ways, then shows which values need quotes, checked with Python.

Syntax​

Syntax
<key>: <plain text>
<key>: '<text in single quotes>'
<key>: "<text in double quotes, with \n escapes>"
  • Plain: no quotes. Fine for ordinary words.
  • Single quotes: everything inside is taken as it is. Write a ' inside as ''.
  • Double quotes: allow escapes such as \n for a new line.

The YAML 1.2.2 specification says of double quotes: "This is the only style capable of expressing arbitrary strings, by using “\” escape sequences." Of single quotes, it says that "within a single-quoted scalar, such characters need to be repeated. This is the only form of escaping performed in single-quoted scalars."

The Example​

strings.yaml has one string of each kind: a plain shop name, a single-quoted sentence with an apostrophe, and a double-quoted text with a new line in it.

Prerequisites​

  • uv and Python, with PyYAML in a project (uv add pyyaml), to check the file. This guide uses PyYAML 6.0.4 on Python 3.15.2.

Step 1: Write the File​

strings.yaml
plain: Bike shop
single: 'It''s open'
double: "Line one\nLine two"

strings.yaml in a macOS-style code window: plain: Bike shop, single: 'It''s open' with the doubled apostrophe, and double: "Line one\nLine two".

Step 2: Check It with PyYAML​

Save this checker as show.py. It loads any YAML file you give it and prints the result:

show.py
import sys
from pprint import pp

import yaml

with open(sys.argv[1], encoding="utf-8") as file:
pp(yaml.safe_load(file))
Terminal
uv run python show.py strings.yaml
Output
{'plain': 'Bike shop', 'single': "It's open", 'double': 'Line one\nLine two'}

The terminal after uv run python show.py strings.yaml: plain Bike shop, single It's open, double Line one\nLine two.

All three are strings. The doubled '' became one apostrophe, and \n in double quotes became a real line break, shown by Python as \n. The command works unchanged in PowerShell.

When Quotes Are Needed​

Without quotes, these three values aren't text:

needs-quotes.yaml
answer: yes
version: 3.10
empty: null
Output
{'answer': True, 'version': 3.1, 'empty': None}

yes became True, 3.10 became the number 3.1, and null became None. With each value in double quotes, the same file loaded as {'answer': 'yes', 'version': '3.10', 'empty': 'null'}.

Common Mistakes​

MistakeWhat PyYAML didDo this instead
A version number without quotes: version: 3.10Read it as the number 3.1Quote it: version: "3.10"
A colon and a space inside plain text: title: Bikes: a short historymapping values are not allowed here, at line 1, column 13Quote it: title: "Bikes: a short history"
An apostrophe in single quotes: single: 'It's open'The quote ended the string early: expected <block end>, but found '<scalar>'Double it, 'It''s open', or use double quotes

Troubleshooting​

SymptomLikely causeFix
A value arrives as True, False, None or a numberIt's a word or number that YAML recognizesPut it in quotes
mapping values are not allowed here on a text line: inside unquoted textQuote the value

FAQ​

Single or double quotes?​

Single quotes when the text has backslashes you want to keep; double quotes when you need escapes such as \n.

Why did yes become True?​

PyYAML follows YAML 1.1, which counts yes, no, on and off as true and false. The YAML 1.1 boolean type lists them all; quoting a value keeps it text in every version.

Conclusion​

strings.yaml loaded three strings written three ways, while yes, 3.10 and null without quotes became True, 3.1 and None. Quote any value that could look like a boolean, a number or null, and any text with : in it.

Additional Resources​