Scalars: strings, numbers, booleans, null

YAML · Essentials · 10 min

Knowing how YAML guesses types prevents values from silently becoming the wrong type in your app.

A scalar is a single value. Written unquoted (a plain scalar), its type is inferred by the parser's schema. With the YAML 1.2 core schema:

  • integers: 42, -7, 0o17 (octal), 0x1F (hex);
  • floats: 3.14, 1e3, .inf, -.inf, .nan;
  • booleans: true, false (also True, TRUE);
  • null: null, ~, or nothing at all after the colon;
  • everything else is a string: hello world, eu-west-1, v1.2.3.

Quotes force a string: "42" and 'true' are strings. Strings rarely need quotes, but some values do (next module): those that look like another type, or that start with a character that has meaning in YAML such as *, &, !, {, [, @, % or a backtick, or that contain : or #.

Dates like 2026-10-17 are a special case: YAML 1.1 parsers (including PyYAML) turn them into date objects, while the 1.2 core schema keeps them as strings. Quote them if the type matters.

A key with nothing after the colon has the value null, not an empty string. Write "" for an empty string.

Strings in plain style can't span multiple lines in a readable way; multi-line text gets its own syntax.

Example

count: 42              # int
ratio: 0.75            # float
big: 1e6               # float 1000000.0 (a string in YAML 1.1 parsers)
enabled: true          # bool
nothing: ~             # null
empty:                 # null as well, not ""
blank: ""              # empty string
version: "3.10"        # string; unquoted 3.10 would be the float 3.1
zip: "01234"           # string; keeps the leading zero
region: eu-west-1      # string
mode: 0o644            # int 420 in YAML 1.2 (YAML 1.1 parsers want 0644)

Remember

  • Plain scalars are typed by appearance: int, float, bool, null or string
  • Quote a value to force a string, e.g. version: "3.10"
  • An empty value is null, not an empty string

Watch out for

  • Writing python_version: 3.10, which parses as the float 3.1.

Check yourself

  1. What is the value of key in key: (nothing after the colon)?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: yaml.org/spec/1.2.2/#23-scalars