Skip to main content

How to Write true and false in YAML

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A YAML setting that's on or off is written true or false. In PyYAML, which follows YAML 1.1, a whole list of other words count too: yes, no, on, off. So a country code NO, for Norway, quietly becomes False. This guide writes booleans and checks them with Python.

Syntax​

Syntax
<key>: true
<key>: false
  • true and false: the two boolean values. They load as Python's True and False.

The YAML 1.1 boolean type, which PyYAML follows, accepts far more words than these two. Its regular expression is y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF.

The Example​

booleans.yaml says whether a shop delivers and whether it gift-wraps.

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​

booleans.yaml
delivery: true
gift_wrap: false

booleans.yaml in a macOS-style code window: delivery: true and gift_wrap: false.

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 booleans.yaml
Output
{'delivery': True, 'gift_wrap': False}

The terminal after uv run python show.py booleans.yaml: delivery True and gift_wrap False.

true and false became Python's True and False. The command works unchanged in PowerShell.

The Other Words​

This file uses three of the YAML 1.1 words, and one of them is a country code:

yes-no.yaml
delivery: yes
gift_wrap: off
country: NO
Output
{'delivery': True, 'gift_wrap': False, 'country': False}

yes and off became booleans, and so did NO, meant as Norway. In quotes, country: "NO" loaded as the text 'NO'. YAML 1.2 dropped these words: its core schema reads only true | True | TRUE | false | False | FALSE as booleans.

Common Mistakes​

MistakeWhat PyYAML didDo this instead
A two-letter code without quotes: country: NOReturned FalseQuote it: country: "NO"
Answers written as words: delivery: yesReturned True, which may not be what a reader of the file expectsWrite true and false, so every YAML version agrees

Troubleshooting​

SymptomLikely causeFix
A text setting arrives as True or FalseIts value is one of the YAML 1.1 boolean wordsQuote it

FAQ​

Does case matter?​

Not for the forms in the list. True and FALSE loaded as True and False, just like true and false.

Why do other YAML tools treat yes differently?​

Because they follow YAML 1.2, which reads yes as text. Writing true and false gives the same result in both.

Conclusion​

delivery: true and gift_wrap: false loaded as True and False. In PyYAML, yes, off and NO did too, so write booleans as true and false, and quote codes that look like one of those words.

Additional Resources​