Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

yaml

How to represent strings in YAML

Tarun Telang

Strings

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"
Representing strings with quotes in YAML

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 scalars
hello world
Representing strings without quotes in YAML

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.

Representing strings in YAML

In YAML, strings can be represented in the following several ways:

  1. Single-quoted scalars
  2. Double-quoted scalars
  3. Unquoted scalars
  4. Folded scalars
  5. Literal scalars

Let's look into each of these ways of representing strings in detail.

Single-quoted scalars

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!'
Representing quoted string in YAML

Double-quoted 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!"
Representing double-quoted string in YAML

Unquoted scalars

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!
Representing string in YAML as unquoted scalars

Folded scalars

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 string 
 spanning multiple lines.
Representing strings in YAML as folded scalars

Literal scalars

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 string 
 spanning multiple lines.
Representing strings in YAML as literal scalars

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.

Conclusion

We learned how to represent strings literals in YAML. We also learned about the different string styles and when to use them.

RELATED TAGS

yaml
RELATED COURSES

View all Courses

Keep Exploring