Start learning free

LESSON 8 OF 414 · WELCOME TO SCALA

String Concatenation in Scala: the + Operator and s Strings

Lesson Read the full explanation

We can concatenate strings with the plus operator (+).

Here is a program that concatenates the strings "I love coding. " and "It is fun!" and assigns the resulted string to a variable message:

val message = "I love coding. " + "It is fun!"
println(message)

Output:

I love coding. It is fun!

Concatenation with variables:

String concatenation is redundant when used with string literals because we already know what the resulted string would be.

For example, in our program, we could have initialized message directly with the bigger string instead of doing concatenation:

val message = "I love coding. It is fun!"
println(message)

String concatenation is used to dynamically generate messages that can be different depending on context.

For example, I might want to print a specific message about my favorite animal, whatever that is:

val favoriteAnimal = "parrot"

val message = "My favorite animal is the " + favoriteAnimal

println(message)

Output:

My favorite animal is the parrot

The value of message will differ if my favorite animal was "dog" or "cat", but it would still make sense.


Concatenating multiple strings:

We can concatenate as many strings as we want in the same line of code by adding a + before each string:

val animal = "parrot"
val name = "Andy"

println(name + ", that's a nice " + animal + " you got there!")

Output:

Andy, that's a nice parrot you got there!

Be careful to add whitespaces and other characters you want in your message


Assignment
Follow the Coding Tutorial and let's practice with string concatenation!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · WELCOME TO SCALA

    String Concatenation in Scala, explained

    Joining strings with +

    Concatenation means gluing strings end to end. In Scala the + operator does it when its left side is a string:

    val message = "I love coding. " + "It is fun!"
    println(message)
    
    I love coding. It is fun!
    

    With two literals this is pointless, since you could write the long string directly. Concatenation earns its place when some parts are variables, so the message changes with the data:

    val animal = "parrot"
    val name = "Andy"
    println(name + ", that's a nice " + animal + " you got there!")
    
    Andy, that's a nice parrot you got there!
    

    Change animal to "dog" and the same line prints a sentence about a dog.

    The exercise step by step

    The starting code defines two values and prints nothing:

    val name: String = "John Doe"
    val language: String = "Python"
    

    The task is to print My name is John Doe and I code in Python by building the sentence from these values.

    1. Start with the fixed text. The sentence begins with "My name is ", including the space before the closing quote.
    2. Add the first variable. Put + name after it, without quotes, so Scala uses the variable's value instead of the word "name".
    3. Add the next piece of text. " and I code in " needs a space on both sides, because nothing else will supply them.
    4. Finish with language. Add + language, again without quotes.
    5. Wrap it in println. The whole expression goes inside the parentheses, so the finished sentence is printed.

    The solution in Scala

    val name: String = "John Doe"
    val language: String = "Python"
    
    println("My name is " + name + " and I code in " + language)
    

    It prints:

    My name is John Doe and I code in Python
    

    Scala evaluates the + operators from left to right, building one longer string at each step.

    Details worth knowing

    • Quotes decide text or variable. "name" is the word name. name is the variable's value.
    • Spaces are your job. If the second piece of text were "and I code in " without the leading space, the output would read John Doeand I code in Python.
    • Numbers can be joined too. When the left side is a string, + converts a number on the right to text: "Age " + 32 gives "Age 32".
    • Order matters with numbers. Scala works from left to right, so 1 + 2 + " items" adds first and gives 3 items, while "items " + 1 + 2 gives items 12.
    • Strings are immutable. + never changes the original strings. It produces a new one.

    The s interpolator

    Long chains of + are easy to get wrong. Scala offers another way: put an s before the opening quote and write $name inside the text.

    println(s"My name is $name and I code in $language")
    

    It prints the same sentence. For an expression rather than a single name, use braces: s"next year: ${age + 1}". Without the s, the text $name is printed literally. This lesson asks for +, so use that.

    Common mistakes

    • Putting a variable name inside the quotes, so the output contains the word instead of the value.
    • Forgetting a space between pieces, which glues two words together.
    • Leaving out a + between two parts, which doesn't compile.
    • Putting a number first in the chain and expecting text: 1 + 2 + " items" is arithmetic until the first string appears.

    Key takeaways

    • + joins strings and creates a new string.
    • Variables go outside the quotes, and you supply the spaces yourself.
    • Evaluation goes left to right, which matters when numbers and strings are mixed.
    • The s interpolator is a tidier alternative for longer messages.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free