Working with Text
7 min · Scala
Join strings, measure them, and use s-string interpolation.
Enemy 1 of 5
Concatenation with +
This code joins two strings with + to make one greeting and prints it. Press Run to see the joined text.
Why this works
Text values (strings) can be joined end-to-end with the same `+` operator used for addition. Scala looks at the types involved and decides whether `+` means "add these numbers" or "glue these strings together." Joining strings this way is called concatenation, and it works with any number of pieces chained in a row: `"a" + "b" + "c"` gives `"abc"`.
This is straightforward for two or three pieces, but it gets visually noisy fast once you're mixing literal text with several variables, which is exactly the problem string interpolation solves.
Rena
Your AI tutor. She walks you through each step.
Asking for help is part of coding. Rena reads your code and gives you a hint. She is free with an account: 20 hints a day.