String Interpolation with 'f'

In the following lesson, you will be introduced to the 'f' string interpolator.

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:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy