Strings are a series of characters, usually represented by letters, numbers, and symbols. They're used to communicate information in textual form.
If we want to represent a string with quotes, we can use either single or double-quotes. For example, both of the following are valid representations of the string "hello world":
# single-quoted scalars'hello world'# double-quoted scalars"hello world"
If we want to represent a string without quotes, we can use the unquoted style. This is how we can represent the same string, "hello world", without using any quotes:
# unquoted scalarshello world
The following are a few rules to follow when using the unquoted style:
1. The string can only contain alphanumeric characters and hyphens.
2. The string cannot start with a number or punctuation character.
3. The string cannot be a keyword in YAML.
In YAML, strings can be represented in the following several ways:
Single-quoted scalars
Double-quoted scalars
Unquoted scalars
Folded scalars
Literal scalars
Let's look into each of these ways of representing strings in detail.
A single-quoted scalar is a basic string with any character except for the single quote ('
). They cannot span multiple lines.
These are useful when we need to represent special characters in our strings. The example of single-quoted scalars is as follows:
'I\'m a YAML string!''I am a YAML string!'
A double-quoted scalar is a more sophisticated string that can have any character, span multiple lines, and use special characters such as the backslash (\
).
These are useful when representing special characters in our strings. The example of double-quoted scalars is as follows:
"I'm a YAML string!"
An unquoted scalar is a simple string that can have any character except for the following:
Special characters: ( { } , : [] & * ? | - > ' " % @ `)
The space character: (
)
The tab character: (\t
)
The line feed character: (\n
)
The carriage return character: (\r
)
The null character: (\0
)
The Unicode non-breaking space character: (\u00A0
)
Unquoted scalars cannot span multiple lines. These are useful when representing data that does not require any special characters. An example of unquoted scalars is as follows:
I'm a YAML string!
The >
character is used to denote folded scalars, which are strings that span multiple lines. The >
character indicates that the following lines are part of the string until a blank line or another line starting with >
is reached. An example of folded scalars is as follows:
> This is a folded stringspanning multiple lines.
The |
character is used to denote literal scalars, which are strings that preserve all whitespace and line breaks. The |
character indicates that the following lines are part of the string until a blank line is reached. An example of literal scalars is as follows:
| This is a literal stringspanning multiple lines.
The |+
character denotes a non-specific tag for a string that preserves all newlines and leading/trailing whitespace.
The |-
character denotes a non-specific tag for a string that removes all trailing empty lines and spaces and leads spaces up to the first non-empty line.
We learned how to represent strings literals in YAML. We also learned about the different string styles and when to use them.
Unlock your potential: YAML basics series, all in one place!
To continue your exploration of YAML basics, check out our series of Answers below:
What is a YAML file?
Understand what YAML files are and the different styles they use.
What is block style in YAML?
Learn the block style format for structuring data in YAML.
What is flow style in YAML?
Discover how flow style differs from block style and when to use it.
How to represent different basic data types in YAML
Explore how YAML handles various basic data types, such as strings, integers, and booleans.
How to represent strings in YAML
Discover how YAML handles string values and different formatting options.
How to represent arrays in YAML
Explore how YAML represents arrays, with each element preceded by a hyphen (-
).
How to represent key-value pairs in YAML
Learn how to effectively represent key-value pairs within YAML syntax.
How to represent maps in YAML
Master how to structure key-value pairs as maps in YAML.
How to represent sequence in YAML?
Understand how to represent sequences in YAML files.
How to represent dictionaries in YAML
Explore how to represent complex data structures like dictionaries in YAML.
How to represent null values in YAML
Learn how to properly represent null or missing values in YAML.
How to write comments in YAML
Learn how to add comments in YAML to improve readability.
What are the advantages of using YAML over other data formats?
Understand why YAML is often preferred over JSON and XML for data serialization.