s, f and raw Interpolators
Embed values in strings.
s, f and raw Interpolators is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 2 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 Interpolation?
String interpolation lets you embed values directly inside a string literal.
Instead of joining pieces with +, you put a prefix letter before the quotes and reference variables inside. Scala has three built-in interpolators: s, f, and raw.
val name = "Ada"
println(s"Hello, $name!")The s Interpolator
The s interpolator is the most common. Put s before the opening quote, then use $variable to insert a value.
It reads more clearly than concatenation with +.
val count = 3
println(s"You have $count messages")Expressions with Braces
For anything more than a simple variable, wrap the expression in ${ }.
This lets you do arithmetic or call methods right inside the string.
val a = 4
val b = 5
println(s"Sum is ${a + b}")
println(s"Upper: ${"hi".toUpperCase}")A Runnable s Example
Here is a complete program using the s interpolator to build a greeting.
Notice how the variables are inserted neatly into the sentence.
object Main extends App {
val user = "Sam"
val items = 2
println(s"Hi $user, you bought $items items.")
}Escaping a Dollar Sign
Inside an interpolated string, $ has special meaning. To print a literal dollar sign, write $$.
This is useful when formatting prices or currency.
val price = 9
println(s"Total: $$$price")The f Interpolator
The f interpolator adds printf-style formatting. After a variable, add a format specifier like %d for integers or %s for strings.
It is great for controlling number precision and alignment.
val n = 42
println(f"Value: $n%d")
val label = "Pi"
println(f"$label%s")Formatting Decimals
Use %f with a precision to control decimal places. For example %.2f shows two digits after the point.
This is ideal for money or measurements.
val pi = 3.14159
println(f"Pi is $pi%.2f")
val ratio = 0.5
println(f"Ratio: $ratio%.1f")Width and Padding with f
The f interpolator can also pad numbers to a width. %5d right-aligns an integer in a field five characters wide.
This helps line up columns of output.
val x = 7
println(f"[$x%5d]")
println(f"[$x%-5d]")The raw Interpolator
The raw interpolator works like s but does not process escape sequences.
So \n stays as the two characters backslash and n, instead of becoming a new line.
println(raw"Path: C:\new\test")
println(s"Path: C:\new\test")Choosing the Right One
Use s for everyday value insertion. Use f when you need number formatting. Use raw when you want backslashes kept literally, like file paths or regex.
All three still support $variable and ${expression}.
val v = 1.0
println(s"s: $v")
println(f"f: $v%.2f")
println(raw"raw: $v\n")Multiple Values Together
You can combine many variables and expressions in one interpolated string.
This keeps output code compact and easy to read compared to long chains of +.
object Main extends App {
val item = "Coffee"
val qty = 2
val cost = 4.5
println(f"$qty x $item = $$${qty * cost}%.2f")
}Quick Check
Which interpolator keeps escape sequences literal?
Recap
You learned Scala's three interpolators. s inserts values with $var and ${expr}.
f adds printf-style formatting like %.2f and %5d. raw behaves like s but keeps escape sequences literal. Use $$ to print a real dollar sign.
Frequently asked questions
Is the “s, f and raw Interpolators” lesson free?
Yes — the full text of “s, f and raw Interpolators” 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 “s, f and raw Interpolators”?
Embed values in strings. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “s, f and raw Interpolators” 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
- String Basics
- s, f and raw Interpolators
- Common String Methods
- Multiline Strings