Skip to content
Theory · Variables and types · lesson 6 of 7

String interpolation

In the previous lesson, a number got into text through String(…) and +. That works, but the line quickly fills up with quotes and plus signs.

And you build such strings all the time. Almost every app shows text with numbers and names inside: "5 minutes left", "Anna, you have 3 new messages". The values change, while the phrase around them stays the same.

Picture a greeting card form: "Hi, ___! You are ___." The text is printed in advance, and the blanks get the name and age of whoever the card goes to. In Swift, such a form is a string with blanks \( ), and Swift fills each blank with a value itself.

Compare two ways to build the same phrase:

SWIFT
let name = "Anna"
let age = 30
print("Hi, " + name + "! Age: " + String(age))  // Hi, Anna! Age: 30
print("Hi, \(name)! Age: \(age)")  // Hi, Anna! Age: 30

Let's walk through both print calls:

  1. The first glues four parts together with +, and it first turns the number age into text with String(age).
  2. The second writes the same phrase as one string. In place of \(name) and \(age), Swift fills in the values of the constants.
  3. Both print Hi, Anna! Age: 30, but the second is shorter, and it shows at once what the phrase will look like.

The blank \( )

String interpolation is putting values into a string through \( ) blanks. The blank \(age) works like this:

  • the backslash \ tells Swift that what follows is a blank, not text;
  • the parentheses hold what to fill in, such as a constant's name;
  • the closing parenthesis ends the blank, and text follows again.

Without the backslash, the parentheses stay plain text: print("Hi, (name)!") prints Hi, (name)!.

Swift turns a value of any type into text on its own: an Int, a Double, a Bool or a Character. You do not need String(…) inside a blank.

An expression in a blank

A blank can hold not only a name but any expression: a piece of code like age + 1 or price * 2 that can be worked out. Swift works it out first and then fills in the result:

SWIFT
let price = 2.5
print("Next year: \(age + 1)")  // Next year: 31
print("Two cost \(price * 2)")  // Two cost 5.0
  1. In \(age + 1), Swift adds 30 and 1 and fills in 31.
  2. In \(price * 2), it multiplies 2.5 by 2. The result has the type Double, so the text gets 5.0, fractional part included.

Here the card picture stops working. A card's blank takes a ready word, but \( ) can hold a calculation: Swift works it out and fills in only the result.

In one way, though, the card is accurate: it is filled in once. Swift fills in the values at the moment the program reaches the line of code with \( ). If the variable changes later, the finished text stays as it was.

Common mistake

A number was glued to text with +:

SWIFT
print("Age: " + age) // ✗
TEXT
error: binary operator '+' cannot be applied to operands of type 'String' and 'Int'

The + sign glues a string only to another string, and age is an Int; Swift will not turn the number into text by itself. Put the number into a blank, "Age: \(age)", or convert it: "Age: " + String(age).

Key points

  • \( ) inside a string is a blank, and Swift fills a value into it.
  • A blank can hold an expression: \(age + 1) fills in the result of the addition.
  • Interpolation turns a value of any type into text on its own: Int, Double, Bool.
  • + glues a string only to another string, so a number goes in through \( ) or String(…).