Skip to main content

How to Write a Comment in YAML

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A comment explains a setting to the next person who reads the file, and programs ignore it. In YAML, a comment starts with #, either on its own line or after a value. The same # catches people out when a value starts with it, such as a colour code. This guide writes both kinds and checks them with Python.

Syntax​

Syntax
# <comment on its own line>
<key>: <value> # <comment after a value>
  • #: starts a comment. Everything after it, to the end of the line, is ignored.
  • A space before #: required when the comment follows a value.

The YAML 1.2.2 specification says: "An explicit comment is marked by a “#” indicator." and "Comments must be separated from other tokens by white space characters."

The Example​

comments.yaml holds a bike shop's name and opening hours, with a comment at the top and one after the hours.

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​

comments.yaml
# Settings for the bike shop
name: Bike shop
open_hours: 8 # from 9 am to 5 pm

comments.yaml in a macOS-style code window: a comment line Settings for the bike shop, name: Bike shop, and open_hours: 8 followed by the comment from 9 am to 5 pm.

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 comments.yaml
Output
{'name': 'Bike shop', 'open_hours': 8}

The terminal after uv run python show.py comments.yaml: the dictionary with name Bike shop and open_hours 8, with no trace of the comments.

Both comments are gone, and open_hours is still the number 8. The command works unchanged in PowerShell.

A Value That Starts with​

A colour code starts with #, and after a space, # begins a comment. This file:

colour.yaml
colour: #ff0000

loaded as:

Output
{'colour': None}

The colour became a comment, and the key was left with no value. In quotes, colour: "#ff0000", it loaded as {'colour': '#ff0000'}.

Common Mistakes​

MistakeWhat happensDo this instead
A value starting with #: colour: #ff0000It loads as None, without an errorQuote it: colour: "#ff0000"
No space before an inline comment: open_hours: 8#noteThe # isn't a comment there, so the value became the text '8#note' instead of the number 8Put a space before #

Troubleshooting​

SymptomLikely causeFix
A setting loads as NoneIts value starts with #, so it became a commentPut the value in quotes

FAQ​

How do I write a comment over several lines?​

Start each line with #.

Do comments reach my program?​

No. The specification says comments "must not be used to convey content information", and safe_load dropped both of them.

Conclusion​

The # lines in comments.yaml vanished on loading, and the settings stayed as they were. A value that begins with # needs quotes, or it becomes a comment too.

Additional Resources​