Skip to main content

Scalar

yaml.org 1.2.2 §10.3

plain | 'single-quoted' | "double-quoted" | | literal | > folded

A YAML scalar is any single value – string, integer, float, boolean, null, or timestamp. YAML infers the type from the value pattern unless an explicit !!tag overrides it. In YAML 1.2, only true and false are booleans; yes/no/on/off are plain strings.

Description

Scalars are the leaf nodes of a YAML document – any value that is not a sequence or mapping. YAML 1.2.2 defines the following core scalar types via the JSON Schema: null (~ or null), boolean (true or false only), integer, float, and string.

Type resolution: YAML infers scalar type from the value. The string "123" resolves to integer 123. "3.14" resolves to float 3.14. "true" resolves to boolean true. To force a type, use an explicit tag: !!str 123 keeps the value as the string "123".

The Norway Problem: YAML 1.1 resolved country codes NO, FI, SE and words like yes, no, on, off as booleans. YAML 1.2 fixed this – only true and false are booleans. PyYAML used YAML 1.1 until version 6.0 (2021). Specify yaml.safe_load() and upgrade to PyYAML 6.0+ to get 1.2 behavior.

Strings do not require quotes unless the value would be misinterpreted. hello is a valid unquoted string. A value like true, 123, or null must be quoted to remain a string: '"true"' or !!str true.

Multi-line strings: the literal block scalar | preserves newlines verbatim. The folded block scalar > folds newlines into spaces (useful for long prose). Both strip a trailing newline by default (use |+ or >+ to keep it, |- or >- to strip all trailing whitespace).

Examples

LabelValue
Plain stringname: Alice
Quoted stringcity: "New York"
Integerport: 8080
Floatratio: 3.14
Boolean (1.2)enabled: true
Nullvalue: null
Null tildevalue: ~
Timestampcreated: 2026-07-24T10:00:00Z
Force string typeport: !!str 8080
Literal blockmessage: | Line one Line two
Folded blocknote: > This sentence folds to one line.

Common Gotchas

!

YAML 1.1 treats yes/no/on/off as booleans – YAML 1.2 does not. PyYAML < 6.0 uses 1.1; always specify yaml.safe_load()

!

Country code NO resolves to boolean false in YAML 1.1 (the Norway Problem). Fixed in YAML 1.2.

!

Unquoted strings that match type patterns are silently coerced – port: 080 may parse as octal 64 in some parsers

!

Tabs are forbidden in YAML indentation – they cause parse errors. Spaces only.

!

A bare colon : followed by a space starts a mapping; use quotes around values containing ' : '

!

Trailing spaces in block scalars are stripped. Significant trailing whitespace requires quoting.

!

YAML documents can contain multiple docs separated by --- ; most parsers read only the first by default

See Also