Start learning free

LESSON 8 OF 348 · WELCOME TO KOTLIN

String Concatenation in Kotlin: The + Operator

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: String = "I love coding. " + "It is fun!"
println(message)

Output:

I love coding. It is fun!

Concatenation with variables:

String concatenation is redundat 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: String = "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: String = "parrot"

val message: String = "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: String = "parrot"
val name: String = "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

    KOTLIN · WELCOME TO KOTLIN

    String Concatenation in Kotlin, explained

    Joining strings with +

    Concatenation means joining strings end to end to make a new one. In Kotlin the + operator does it:

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

    The result is a new string. Neither original string is modified; Kotlin strings can't be changed after they are created. Notice that nothing is added between the two pieces. The space after coding. is part of the first literal. If it were missing, the output would read coding.It.

    Why concatenation is useful

    Joining two literals is pointless, because you could write the longer string directly. Concatenation matters when part of the text comes from a variable:

    val favoriteAnimal: String = "parrot"
    val message: String = "My favorite animal is the " + favoriteAnimal
    println(message)
    
    My favorite animal is the parrot
    

    Change favoriteAnimal to "dog" and the same line builds a different sentence. The quoted part stays fixed, and the variable supplies the part that changes. Inside the line, the variable is written without quotes: quotes would make it the literal text favoriteAnimal instead of its value.

    Chaining several pieces

    You can put as many + operators in one expression as you need:

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

    Kotlin evaluates the pieces from left to right, joining two at a time. The comma, the spaces and the exclamation mark all live inside the literals, because Kotlin adds nothing on its own. When the output looks glued together, a missing space inside a literal is the usual reason.

    The exercise

    The starter code declares two strings:

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

    The task is to print one sentence built from four pieces: "My name is ", the variable name, " and I code in " and the variable language. Build it from the front:

    1. Start with the first literal, "My name is ". It ends with a space, because the name follows directly.
    2. Add name with +. This is the variable, outside any quotes.
    3. Add the second literal, " and I code in ". It starts and ends with a space, one on each side of the word and and before the language.
    4. Add language. The sentence ends with the value, so there is nothing more to append.
    5. Wrap it all in println, so it is printed once.
    val name: String = "John Doe"
    val language: String = "Python"
    
    println("My name is " + name + " and I code in " + language)
    

    This prints My name is John Doe and I code in Python. The sentence follows the variables: change name to "Ada" and language to "Kotlin" and the same println line prints My name is Ada and I code in Kotlin.

    Building a string in steps

    A message can also be assembled over several lines. With a var, the += operator appends to the existing text:

    var greeting = "Hello"
    greeting += ", world"
    greeting += "!"
    println(greeting)
    
    Hello, world!
    

    Each += builds a new string from the old one plus the addition and stores it back in greeting. The old string is not edited, because strings can't be edited. Declare the variable with val and the first += is rejected with 'val' cannot be reassigned. For a handful of pieces this is fine; the rule of thumb is to put everything in one expression when you can.

    Gotchas

    • Spaces are your job. Put them inside the literals. Count them at every joint between a literal and a variable.
    • Variables go outside the quotes. "My name is name" prints the word name, not its value.
    • The left operand sets the rule. With a string on the left, + accepts almost any value on the right and converts it to text. "Age: " + 29 gives Age: 29. With a number on the left, such as 29 + " years", the code doesn't compile, because Int has no + that takes a string.
    • Numbers added to text. "Total: " + 1 + 2 gives Total: 12, since the pieces are joined left to right. To add first, use parentheses: "Total: " + (1 + 2) gives Total: 3.
    • String templates. Kotlin can also put a variable straight into a string with $: "My name is $name and I code in $language" produces the same text with less punctuation. It's usually the more readable form for longer sentences, and the lesson on variables uses it. This exercise practises +.

    Key takeaways

    • + between strings joins them into a new string.
    • It adds no spaces; they must be inside the quotes.
    • Variables are written without quotes and supply the part that changes.
    • Several + operators chain from left to right.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free