Start learning free

LESSON 8 OF 415 · WELCOME TO SWIFT

String Concatenation in Swift: 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:

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

let message = "I love coding. It is fun!"
print(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:

let favoriteAnimal = "parrot"

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

print(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:

let animal = "parrot"
let name = "Andy"

print(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

    SWIFT · WELCOME TO SWIFT

    String Concatenation in Swift, explained

    What concatenation is

    Concatenating strings means joining them end to end into one new string. In Swift you do it with the + operator:

    let message = "I love coding. " + "It is fun!"
    print(message)
    
    I love coding. It is fun!
    

    With two literals this is pointless, since you could write the whole text in one literal. Concatenation earns its place when some parts come from variables, so the message changes with the data:

    let favoriteAnimal = "parrot"
    let message = "My favorite animal is the " + favoriteAnimal
    print(message)
    
    My favorite animal is the parrot
    

    Change favoriteAnimal to "dog" and the sentence follows. Notice that the variable name sits outside the quotes. Inside quotes, favoriteAnimal would be the literal letters, not the value.

    Joining several pieces

    You can chain as many + operators as you like. Swift evaluates them from left to right and produces a single string:

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

    Concatenation adds nothing between the pieces. Spaces, commas and punctuation must be inside one of the literals. Forgetting the space is the most common slip:

    let name = "Andy"
    print("Hello" + name)
    
    HelloAndy
    

    Building a string in steps

    When a message grows over several statements, keep it in a var and append to it. Each += adds to the end of the current value:

    var sentence = "Swift"
    sentence += " is"
    sentence += " fun"
    print(sentence)
    
    Swift is fun
    

    Joining a first name, a space and a last name works the same way: "Ada" + " " + "Lovelace" gives "Ada Lovelace", a string of 12 characters. The space is a character like any other, and it counts.

    The exercise

    The starter code declares two constants, name and language. The task is to print one sentence built from four pieces: the text "My name is ", the constant name, the text " and I code in ", and the constant language.

    1. Start a print(...) call. The whole concatenation goes inside the parentheses, so one string is printed.
    2. Write the first literal, "My name is ", with its trailing space inside the quotes.
    3. Add + name. The constant goes outside quotes, because we want its value.
    4. Add + " and I code in ". This literal has a space on both sides, one to separate it from the name and one to separate it from the language.
    5. Finish with + language.

    The solution in Swift

    let name = "John Doe"
    let language = "Python"
    
    print("My name is " + name + " and I code in " + language)
    

    It prints My name is John Doe and I code in Python. Because the sentence is built from the constants, changing language to another value changes the output without touching the print line.

    Swift details

    • Only strings join with +. Swift does not convert numbers for you. With let age = 29, the line "Age: " + age fails with binary operator '+' cannot be applied to operands of type 'String' and 'Int'. Convert first, "Age: " + String(age), or use interpolation.
    • Interpolation is often tidier. "My name is \(name) and I code in \(language)" produces the same sentence, puts the variables inside the text and needs no manual spacing between pieces. It also accepts numbers directly.
    • += appends. With var s = "ab", the statement s += "cd" makes s equal to "abcd". The variable must be declared with var, because let constants cannot change.
    • Strings are values. Concatenation builds a new string. The original pieces stay as they were.

    Common mistakes

    • Missing spaces. "Hello" + name gives HelloAndy. Put the space inside the literal: "Hello " + name.
    • Quoting a variable. "name" is the word name, not the value of the constant.
    • Leaving out a +. Two pieces side by side without an operator do not compile.
    • Mixing in numbers. Convert with String(...) or use interpolation.

    Key takeaways

    • + joins two strings into a new string.
    • Variables go outside quotes; spaces and punctuation go inside them.
    • Numbers must be converted to strings, or interpolated, before they join a sentence.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free