0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

String Interpolation

s, f, and raw.

String Interpolation 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 Is String Interpolation?

String interpolation lets you embed values directly inside a string literal instead of concatenating with +.

You prefix the string with a letter like s, f, or raw, then use $ to insert values.

The s Interpolator

The s interpolator inserts the value of a variable where you write $name.

It is the most common interpolator and replaces clunky concatenation.

object Main {
  def main(args: Array[String]): Unit = {
    val name = "Ada"
    println(s"Hello, $name!")
  }
}

Expressions With Braces

For anything more than a simple variable, wrap the expression in ${ ... }.

The braces tell the interpolator where the expression begins and ends.

object Main {
  def main(args: Array[String]): Unit = {
    val a = 4
    val b = 6
    println(s"Sum is ${a + b}")
  }
}

Calling Methods Inside

You can call methods inside ${ ... } too. Any valid expression works.

object Main {
  def main(args: Array[String]): Unit = {
    val word = "scala"
    println(s"Upper: ${word.toUpperCase}")
  }
}

The f Interpolator

The f interpolator adds printf-style formatting. After the expression you give a format like %.2f for two decimal places.

This is ideal for money, percentages, and aligned output.

object Main {
  def main(args: Array[String]): Unit = {
    val price = 9.5
    println(f"Price: $$$price%.2f")
  }
}

Formatting Integers

The f interpolator also formats integers, for example %d for a decimal integer or %05d to pad with zeros.

object Main {
  def main(args: Array[String]): Unit = {
    val id = 42
    println(f"ID: $id%05d")
  }
}

The raw Interpolator

The raw interpolator works like s but does not process escape sequences.

So \n stays as the two characters backslash-n instead of becoming a newline.

object Main {
  def main(args: Array[String]): Unit = {
    println(raw"line1\nline2")
  }
}

Comparing s and raw

With s, the escape \n becomes a real newline. With raw, it stays literal.

Use raw for paths, regex patterns, or anything where backslashes should be preserved.

object Main {
  def main(args: Array[String]): Unit = {
    println(s"a\nb")
    println(raw"a\nb")
  }
}

Multi-line Interpolation

Interpolation also works with triple-quoted strings, which can span multiple lines.

This is great for building templated, readable output.

object Main {
  def main(args: Array[String]): Unit = {
    val user = "Grace"
    val score = 95
    println(s"""User: $user
Score: $score""")
  }
}

Escaping a Dollar Sign

To print a literal dollar sign inside an interpolated string, write $$.

This avoids the interpolator treating it as the start of a variable.

object Main {
  def main(args: Array[String]): Unit = {
    val amount = 20
    println(s"Cost: $$$amount")
  }
}

Putting It Together

Combine plain variables, an expression in braces, and a method call in one interpolated message.

object Main {
  def main(args: Array[String]): Unit = {
    val item = "book"
    val qty = 3
    val each = 12.0
    println(s"$qty ${item}s cost ${qty * each}")
  }
}

Quick Check

Test your knowledge of interpolators.

Recap

You learned Scala's three main string interpolators:

  • s inserts variables and ${expr} expressions
  • f adds printf-style formatting like %.2f and %05d
  • raw keeps escape sequences literal
  • Use $$ for a literal dollar sign
  • Interpolation works with triple-quoted multi-line strings too

Frequently asked questions

Is the “String Interpolation” lesson free?

Yes — the full text of “String Interpolation” 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 “String Interpolation”?

s, f, and raw. 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 “String Interpolation” 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. Using the REPL
  2. val, var and Types
  3. Expressions over Statements
  4. String Interpolation
← Back to Scala for Backend Engineering & Functional Programming