0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Multiline Strings

Use triple-quoted blocks.

Multiline Strings is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 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 Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Multiline Strings?

Sometimes you need text that spans several lines, like a message, JSON, or SQL query.

Scala lets you write these with triple quotes, which preserve line breaks exactly as you type them.

val text = """Line one
Line two"""
println(text)

Triple-Quoted Syntax

Wrap the text in three double quotes on each side: """...""".

Inside, you do not need to escape regular double quotes, which makes it ideal for text that contains quotes.

val json = """{"name": "Ada"}"""
println(json)

Real Line Breaks

In a triple-quoted string, pressing enter creates an actual new line in the value.

This is great for blocks of text where you want the layout preserved.

object Main extends App {
  val poem = """Roses are red
Scala is neat"""
  println(poem)
}

The Indentation Problem

If you indent a multiline string in your code, those leading spaces become part of the value.

That often looks messy when printed. Scala offers a clean way to strip the indentation.

val msg = """Hello
    World"""
println(msg)

stripMargin

stripMargin removes leading whitespace up to a margin character, which is | by default.

Put a | at the start of each line, then call stripMargin to align your code neatly.

val msg = """Hello
    |World""".stripMargin
println(msg)

A Clean Multiline Block

Combining triple quotes with stripMargin gives readable code and clean output.

Each line starts with |, which is stripped away when printed.

object Main extends App {
  val report = """Report
    |Total: 5
    |Done!""".stripMargin
  println(report)
}

Custom Margin Character

You can pass a different margin character to stripMargin if | conflicts with your text.

For example, use # as the margin marker instead.

val s = """One
    #Two""".stripMargin('#')
println(s)

Multiline with Interpolation

You can combine triple quotes with the s interpolator to insert values into multiline text.

Put s before the opening triple quote, then use $variable as usual.

val name = "Sam"
val msg = s"""Hi $name,
    |Welcome aboard!""".stripMargin
println(msg)

Building a SQL Query

Multiline strings shine when writing SQL or other long text in backend code.

The layout stays readable in the source and in the output.

val query = """SELECT id, name
    |FROM users
    |WHERE active = true""".stripMargin
println(query)

Quotes Inside Quotes

Inside triple quotes, normal double quotes need no escaping. This avoids the clutter of writing \" everywhere.

It is perfect for embedding JSON or quoted phrases.

val line = """He said "hello" to me"""
println(line)

Combining With Methods

A multiline string is still a normal String, so all the usual methods work on it.

Here we trim and count the length after stripping the margin.

val block = """abc
    |def""".stripMargin
println(block.length)
println(block.toUpperCase)

Quick Check

How do you remove leading indentation from a multiline string?

Recap

You learned to write multiline text with triple quotes """...""", which preserve line breaks and need no quote escaping.

You used stripMargin with a | marker to keep code tidy, combined it with the s interpolator, and applied normal string methods to the result.

Frequently asked questions

Is the “Multiline Strings” lesson free?

Yes — the full text of “Multiline Strings” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming 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 Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Multiline Strings”?

Use triple-quoted blocks. You practise Scala for Backend Engineering & Functional Programming 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 Scala for Backend Engineering & Functional Programming?

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

How long does the “Multiline Strings” 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 Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming 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. String Basics
  2. s, f and raw Interpolators
  3. Common String Methods
  4. Multiline Strings
← Back to Scala for Backend Engineering & Functional Programming