How to Represent Multi-Line Strings in YAML

YAML (YAML Ain't Markup Language) provides a straightforward way to represent multi-line strings, allowing developers to include longer text blocks in a readable format. There are two primary methods for defining multi-line strings in YAML: using the pipe (|) and the greater-than sign (>).

1. Using the Pipe (|) for Literal Block Style

The pipe symbol (|) is used to indicate that the following lines should be treated as a literal block. This means that all line breaks and indentation will be preserved in the resulting string.

        
description: |
This is a multi-line string
that preserves line breaks.
Each line will be included as-is.

In this example:

  • description is the key representing the multi-line string.
  • The text following the pipe symbol is treated as a literal block, preserving the line breaks and indentation.

2. Using the Greater-Than Sign (>) for Folded Block Style

The greater-than sign (>) is used to indicate that the following lines should be treated as a folded block. This means that line breaks will be replaced with spaces, and the text will be presented as a single line, unless there are double line breaks, which will create a new paragraph.

        
summary: >
This is a multi-line string
that will be folded into a single line
when processed, except for double line breaks.

This creates a new paragraph.

In this example:

  • summary is the key representing the multi-line string.
  • The text following the greater-than sign is treated as a folded block, resulting in a single line of text with spaces replacing the line breaks.
  • A double line break creates a new paragraph in the output.

3. Combining Multi-Line Strings with Other Data Types

Multi-line strings can be combined with other data types in YAML, such as lists and dictionaries. Here’s an example:

        
article:
title: "Understanding YAML"
content: |
YAML is a human-readable data serialization format.
It is commonly used for configuration files and data exchange.
tags:
- YAML
- Data Serialization
- Configuration

In this example:

  • article is a dictionary containing a title, a content (multi-line string), and a list of tags.
  • The content key uses the pipe symbol to preserve line breaks in the text.

4. Important Points to Remember

  • Use the pipe (|) for literal blocks when you want to preserve line breaks.
  • Use the greater-than sign (>) for folded blocks when you want to convert line breaks into spaces.
  • Indentation is important for multi-line strings, and it should be consistent with the surrounding structure.

Conclusion

In summary, YAML provides two effective ways to represent multi-line strings: using the pipe (|) for literal blocks and the greater-than sign (>) for folded blocks. These features enhance the readability and organization of text data in YAML documents, making it easier to manage complex configurations and content.