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.
- Start a
print(...)call. The whole concatenation goes inside the parentheses, so one string is printed. - Write the first literal,
"My name is ", with its trailing space inside the quotes. - Add
+ name. The constant goes outside quotes, because we want its value. - 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. - 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. Withlet age = 29, the line"Age: " + agefails withbinary 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. Withvar s = "ab", the statements += "cd"makessequal to"abcd". The variable must be declared withvar, becauseletconstants cannot change.- Strings are values. Concatenation builds a new string. The original pieces stay as they were.
Common mistakes
- Missing spaces.
"Hello" + namegivesHelloAndy. 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.
