String Interpolation
Building strings out of variables and expressions with Swift's \(...) syntax.
읽는 데 2분
Swift builds strings out of values using string interpolation — embedding an expression directly inside a string literal with \(...).
let name = "Aisha"
let age = 28
let message = "\(name) is \(age) years old."
print(message) // "Aisha is 28 years old."This is the idiomatic way to combine text and values in Swift — reach for it instead of manually concatenating pieces with +.
Interpolating expressions, not just variables
Anything that evaluates to a value can go inside \(...), not just a plain variable name:
let price = 19.99
let quantity = 3
print("Total: $\(price * Double(quantity))") // "Total: $59.97"
let items = ["pen", "notebook", "eraser"]
print("You have \(items.count) items.") // "You have 3 items."This means you rarely need a separate step to "build up" a string before printing or displaying it — the calculation and the string can be written in one place.
Concatenation still exists, but interpolation usually reads better
let first = "Hello"
let second = "World"
let concatenated = first + ", " + second + "!" // works, but gets noisy
let interpolated = "\(first), \(second)!" // reads more like the outputOnce more than one or two pieces are involved, + concatenation turns into a string of quotes and plus signs that's harder to scan than the interpolated version, which visually resembles the sentence it produces.
Formatting numbers inside interpolation
By default, interpolating a Double prints its full value, which isn't always what you want for things like currency:
let total = 19.999
print("Total: $\(total)") // "Total: $19.999"
print("Total: $\(String(format: "%.2f", total))") // "Total: $20.00"String(format:) borrows printf-style format specifiers — %.2f rounds to two decimal places — which comes up often when displaying prices or measurements to users.
Why this matters beyond convenience
String interpolation isn't just shorter to type — it keeps the shape of the output visible in the code itself. Reading "\(name) is \(age) years old." tells you exactly what the final string looks like at a glance, where a chain of concatenated pieces forces you to mentally reassemble it. As you write more Swift, you'll find interpolation is used everywhere: building log messages, constructing UI labels, and formatting data for display all lean on it constantly.