0Pricing
Swift Academy · Lesson

Multiline String Literals

Write readable multiline text with triple quotes.

Multiline String Literals is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Multiline String Literals

A multiline string literal spans several lines. It is delimited by three double-quote marks on their own lines, and the text in between keeps its line breaks.

let poem = """
Roses are red
Violets are blue
"""
print(poem)

Preserving Line Breaks

Each newline you type inside the literal becomes a newline in the resulting string, so formatting is preserved exactly.

let menu = """
Coffee
Tea
Water
"""
print(menu)

Indentation Stripping

The whitespace before the closing triple-quote sets the baseline indentation. Swift strips that much leading whitespace from every line.

func describe() -> String {
    return """
    Title
    Body
    """
}
print(describe())

Keeping Some Indentation

Indentation beyond the closing-delimiter baseline is kept. This lets you indent nested content intentionally.

let tree = """
Root
    Child
    Child
"""
print(tree)

No Leading or Trailing Newline

The line break right after the opening """ and right before the closing """ are not part of the string.

let s = """
Hello
"""
print("[\(s)]")

Interpolation in Multiline Strings

Multiline literals support interpolation just like single-line strings.

let name = "Sam"
let note = """
Dear \(name),
Welcome aboard.
"""
print(note)

Escaping a Line Break

Ending a line with a backslash joins it to the next line, suppressing that newline in the output.

let joined = """
This is one \
long line
"""
print(joined)

Quotes Without Escaping

Inside a multiline literal you can write single double-quote characters freely without escaping them.

let quote = """
She said "hello" today
"""
print(quote)

Building Templates

Multiline strings are ideal for templates such as emails, code snippets, or formatted reports.

let user = "Lee"
let total = 42
let email = """
Hi \(user),
Your total is \(total).
Thanks!
"""
print(email)

Combining with Formatting

You can interpolate formatted values inside multiline templates for clean, aligned output.

let price = 9.5
let receipt = """
Item: Coffee
Price: \(String(format: "%.2f", price))
"""
print(receipt)

Why Use Multiline Literals

They keep multi-line text readable in source code without manual newline characters, reducing errors and improving clarity.

let help = """
Usage:
  run start
  run stop
"""
print(help)

Quick Check

Recall how indentation is handled.

Recap: Multiline String Literals

You learned that triple-quote literals preserve line breaks, that the closing delimiter indentation sets how much leading whitespace is stripped, that the surrounding newlines are excluded, and that interpolation and a trailing backslash to join lines both work inside them.

let card = """
Name: Ada
Role: Engineer
"""
print(card)

Frequently asked questions

Is the “Multiline String Literals” lesson free?

Yes — the full text of “Multiline String Literals” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Multiline String Literals”?

Write readable multiline text with triple quotes. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Multiline String Literals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Basic String Interpolation
  2. Formatting Numbers in Strings
  3. Multiline String Literals
  4. Custom String Interpolation
← Back to Swift Academy