The f String Interpolator
Syntax
printf-Style Formatting
Syntax
Flag
Width
Precision
Conversion-Characters
Learn by Example
Remember when we discussed the printf method when learning how to print? printf is actually a Java method which is recognized by Scala. However, it is unconventional to use printf in Scala as it has its own, arguably better, printf.
The f String Interpolator
The f string interpolator is Scala’s printf. For string interpolation with f, we prepend an f to any string literal. This allows us to create formatted strings. When using the f interpolator, all references to variables and expressions should be followed by a printf-style format string, like %f.
Let’s look at an example: