Questions And Answers

More Tutorials

Scala String Interpolation


Hello String Interpolation

The s interpolator allows the usage of variables within a string.

val name = "Brian"
println(s"Hello $name")

prints "Hello Brian" to the console when ran.

Formatted String Interpolation Using the f Interpolator

val num = 42d

Print two decimal places for num using f


Print num using scientific notation using e


Print num in hexadecimal with a


Using expression in string literals

You can use curly braces to interpolate expressions into string literals:

def f(x: String) = x + x
val a = "A"
s"${a}" // "A"
s"${f(a)}" // "AA"

Without the braces, scala would only interpolate the identifier after the $ (in this case f). Since there is no implicit conversion from f to a String this is an exception in this example:

s"$f(a)" // compile-time error (missing argument list for method f)

Custom string interpolators

It is possible to define custom string interpolators in addition to the built-in ones.


Is expanded by the compiler to:

new scala.StringContext("foo", "baz").my(bar)

scala.StringContext has no my method, therefore it can be provided by implicit conversion. A custom interpolator with the same behavior as the builtin s interpolator would then be implemented as follows:

implicit class MyInterpolator(sc: StringContext) {
 def my(subs: Any*): String = {
 val pit =
 val sit = subs.iterator
 // Note parts.length == subs.length + 1
 val sb = new java.lang.StringBuilder(
 while(sit.hasNext) {

And the interpolation my"foo${bar}baz" would desugar to:

new MyInterpolation(new StringContext("foo", "baz")).my(bar)

Note that there is no restriction on the arguments or return type of the interpolation function. This leads us down a dark path where interpolation syntax can be used creatively to construct arbitrary objects, as illustrated in the following example:

case class Let(name: Char, value: Int)
implicit class LetInterpolator(sc: StringContext) {
 def let(value: Int): Let = Let(, value)
let"a=${4}" // Let(a, 4)
let"b=${"foo"}" // error: type mismatch
let"c=" // error: not enough arguments for method let: (value: Int)Let


In this page (written and validated by ) you learned about Scala String Interpolation . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Symbol Literals.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.