String Interpolation
s, f, and raw.
String Interpolation is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit. This is 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, and your progress syncs across the web and the CoddyKit app. The Scala for Backend Engineering & Functional Programming course includes 4 lessons in total.
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:
sinserts variables and${expr}expressionsfadds printf-style formatting like%.2fand%05drawkeeps escape sequences literal- Use
$$for a literal dollar sign - Interpolation works with triple-quoted multi-line strings too
Learn Scala 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
- 39
- Lessons
- 143
Frequently Asked Questions
Is the “String Interpolation” lesson free?
Yes — the full text of “String Interpolation” is free to read here on the web. 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. The Scala for Backend Engineering & Functional Programming course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- Using the REPL
- val, var and Types
- Expressions over Statements
- String Interpolation