Skip to main content

How to Write a Key and a Value in YAML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

YAML files are made of key-value pairs: a name, a colon, a space and the value, such as city: Leeds. A set of them is called a mapping, and programs read it as a dictionary. This guide writes two pairs, checks them with Python, and shows what four small mistakes really do.

Syntax​

Syntax
<key>: <value>
<key>: <value>
  • <key>: the setting's name.
  • : : a colon followed by a space. The space is required.
  • <value>: the setting's value, here plain text.
  • One pair per line, each starting at the left edge.

The YAML 1.2.2 specification puts it in one sentence: "Mappings use a colon and space (“: ”) to mark each key/value pair."

The Example​

A file called shop.yaml describes a bike shop with two pairs: its name and its city. A short Python script loads it with PyYAML and prints what it read.

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​

Create a file called shop.yaml with this text:

shop.yaml
name: Bike shop
city: Leeds

shop.yaml in a macOS-style code window: two key-value pairs, name: Bike shop and city: Leeds.

Step 2: Check It with PyYAML​

This script 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))

Run it on the file:

Terminal
uv run python show.py shop.yaml
Output
{'name': 'Bike shop', 'city': 'Leeds'}

The terminal after uv run python show.py shop.yaml: the dictionary with name Bike shop and city Leeds.

The two pairs became a dictionary with two keys. The value Bike shop kept its space, without quotes. The command works unchanged in PowerShell.

Common Mistakes​

I wrote each mistake into its own small file and loaded it with show.py. These are PyYAML 6.0.4's real results:

MistakeExampleWhat PyYAML did
No space after the colonname:Bike shopNo error, but no pair either: it read the whole line as one piece of text, 'name:Bike shop'
The same key twicename: Bike shop, then name: Cycle storeNo error: the last one won, {'name': 'Cycle store'}
A tab after the colonname: followed by a tabfound character '\t' that cannot start any token

The first two are the dangerous ones: the file loads, but not the way you meant. The specification says a mapping's keys must be unique, "with the restriction that each of the keys is unique", but PyYAML doesn't enforce it.

Troubleshooting​

SymptomLikely causeFix
Your program gets text where it expected a dictionaryA colon without a space after itAdd the space: name: Bike shop
A setting has an unexpected valueThe key appears twice, and the later one winsSearch the file for the key
found character '\t' that cannot start any tokenA tab characterUse spaces only

FAQ​

Do keys need quotes?​

No, not for simple names like name or open_hours.

Why can't YAML use tabs?​

The specification says "To maintain portability, tab characters must not be used in indentation, since different systems treat tabs differently."

Conclusion​

name: Bike shop and city: Leeds loaded as a dictionary with two keys. A colon needs a space after it, keys should appear once, and tabs don't belong in YAML.

Additional Resources​