Scalars¶
Scalars are single, indivisible values. They can be strings
, numbers
, booleans
, or null
.
Strings (!!str
) 📜¶
Strings in YAML are used to represent textual data. YAML allows you to format strings in various ways to handle simple and complex text scenarios.
Plain Strings¶
Plain strings are unquoted strings. They are the simplest and most commonly used.
yml
name: chaitu-ycr
description: This is a plain string
Quoted Strings¶
Use single quotes for strings that include special characters that should be taken literally.
yml
text: 'It''s a sunny day' # Escapes single quote
emoji: '😀😃😄'
Use double quotes for strings that include special characters or need to include escape sequences
yml
text: "Hello, \nWorld!" # \n creates a newline
emoji: "😀😃😄"
Multiline Strings 🖋️¶
YAML supports multiline strings using block (|
) and folded (>
) styles.
yml
block_scalar: |
This is a block scalar.
It preserves line breaks.
Even empty lines are included.
folded_scalar: >
This is a folded scalar.
It folds new lines into spaces,
creating a single flowing paragraph.
Special Characters and Escaping 🔍¶
Double-quoted strings can contain special characters and escape sequences.
Use backslash (\
) to introduce escape sequences.
yml
special_characters: "Newline: \n, Tab: \t, Backslash: \\, Quote: \""
Null Strings 🌀¶
Null values are represented as (null
) or (~)
.
yml
null_example1: null
null_example2: ~
Numbers (!!int
, !!float
) 🔢¶
YAML supports different types of numbers, including integers, floating-point numbers, and numbers in scientific notation.
example
yml
# An integer representing age
age: 30
# A negative integer representing temperature
temperature: -5
# A float representing height in feet
height: 5.9
# Speed of light in meters per second
speed_of_light: 3.0e8
# A hexadecimal color code
color_code: 0xFF5733
# File permission notation
file_permissions: 0755
# A binary number
binary_value: 0b101010
Booleans (!!bool
) ⚖️¶
Booleans in YAML are used to represent true or false values. They are essential for controlling logic and conditions within your YAML files.
- True Values:
true
,True
,TRUE
,yes
,Yes
,YES
,on
,On
,ON
- False Values:
false
,False
,FALSE
,no
,No
,NO
,off
,Off
,OFF
yml
# Standard Boolean Values
is_active: true # Basic boolean
has_license: false # Basic boolean
# Alternative Representations
is_enabled: yes # Alternative for true
is_disabled: no # Alternative for false
# Switch States
light_switch: on # Alternative for true
dark_mode: off # Alternative for false
Null (!!null
) 🌀¶
Null values in YAML represent the absence of a value. They are useful for indicating missing, undefined, or unassigned data.
- example
yml
# Explicit (null)
preferred_language: null # Language preference not set
# Tilde (~)
shipping_address: ~ # Shipping address is missing
# Empty Value: Just leave the value empty after the colon.
phone_number: # Phone number not provided
# Empty Quotes: Using single or double quotes.
additional_info: '' # No additional information
middle_name: ""