Swift Academy · Lesson

Multiline String Literals

Write readable multiline text with triple quotes.

Lesson 3 of 413 steps

Multiline String Literals is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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)
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Multiline String Literals” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Multiline String Literals”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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