In this episode of Swift Fundamentals, we look at string interpolation in Swift. You learn about the syntax, and we look at the benefits and drawbacks of string interpolation in your code.

What Is String Interpolation?

Most modern programming languages support string interpolation, and Swift is no exception. String interpolation is nothing more than substituting values of constants, variables, literals, and expressions into placeholders in a string. You are inserting or interpolating values, literals, or expressions within a string literal hence the term string interpolation.

While string interpolation syntax differs from programming language to programming language, each programming language with string interpolation support defines a character or sequence of characters to define the start of a placeholder. When the string is evaluated, the placeholder is replaced by the value of the constant or variable or the result of the expression.

String Interpolation Syntax

Let's use an example to understand the syntax for string interpolation in Swift. Fire up Xcode and create a playground by choosing the Blank template from the iOS > Playground section if you want to follow along.

String Interpolation in Swift

In this example, we define a constant with name name and assign a string literal to it, Bart. We insert the value of the constant into the string by wrapping the name of the constant in parentheses (()) and prefixing the parentheses with a backslash (\).

import Foundation

let name = "Bart"
let greeting = "My name is \(name)."

The contents of the parentheses can be anything that returns a value, a constant, a variable, a literal, or an expression. In this example, we insert the result of an expression into a string literal.

import Foundation

let amount = 15.5
let tax = 5.25
let invoice = "The total cost is \(amount + (amount * tax))."

Escaping Special Characters

The \() pattern is what the compiler looks for when it inserts values into a string literal. The string literal may contain this pattern and you want the compiler to ignore it. You can do that by escaping the \() pattern and other special characters with two backslashes. In this example, we explain how string interpolation works so we need to escape the \() characters in the string literal.

import Foundation

let name = "Bart"
let greeting = "Use \\() in the string literal to insert a value."

The value of greeting includes the \() pattern because the two backslashes are replaced with a single backslash, as shown below.

Use \\() in the string literal to insert a value.

The two backslashes can also be used to use quotation marks in the string literal. This example may seem a bit complex, but it really isn't. We wrap the name in quotation marks by escaping each quotation mark with a backslash.

import Foundation

let name = "Bart"
let greeting = "Hello \"\(name)\""

The result is a string that wraps the name in quotation marks.

Hello "Bart"

Benefits and Drawbacks

Because Swift automatically converts objects to a string representation, string interpolation just works. It can improve readability and make your code more flexible. I use it very often for logging.

print("Request Failed for \(path) with \(statusCode)")

I don't recommend using string interpolation for localization, though. The order of words differs between languages and that inevitably results in incorrect sentences. The result is a poor user experience.

What's Next?

The message is simple. Do use string interpolation, but don't use it for localization. The syntax is easy to pick up, and performance issues are rare unless you create complex strings with many interpolated values.