How to Write a Key and a Value in YAML
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
<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:
name: Bike shop
city: Leeds

Step 2: Check It with PyYAML
This script loads any YAML file you give it and prints the result:
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:
uv run python show.py shop.yaml
{'name': 'Bike shop', '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:
| Mistake | Example | What PyYAML did |
|---|---|---|
| No space after the colon | name:Bike shop | No error, but no pair either: it read the whole line as one piece of text, 'name:Bike shop' |
| The same key twice | name: Bike shop, then name: Cycle store | No error: the last one won, {'name': 'Cycle store'} |
| A tab after the colon | name: followed by a tab | found 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
| Symptom | Likely cause | Fix |
|---|---|---|
| Your program gets text where it expected a dictionary | A colon without a space after it | Add the space: name: Bike shop |
| A setting has an unexpected value | The key appears twice, and the later one wins | Search the file for the key |
found character '\t' that cannot start any token | A tab character | Use 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
- YAML 1.2.2 specification — the full language.
- PyYAML documentation — the Python library used to check the files.
