100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

String Interpolation in Swift

See how Swift's backslash-parenthesis syntax embeds expressions inside string literals, including multi-line triple-quoted strings.

BasicsBeginner6 min readJul 8, 2026
Analogies

Introduction

String interpolation is a way to build a new String value by mixing constants, variables, literals, and expressions directly inside a string literal. Instead of concatenating pieces of text with the + operator, Swift lets you embed a value inside a string using a backslash followed by parentheses. Swift also supports multi-line string literals delimited by triple double-quotes, which can contain interpolation as well.

🏏

Cricket analogy: String interpolation is like a commentator announcing a live score by weaving the batsman's name and current runs directly into a sentence, instead of separately stitching together stat cards with a '+' operator; the triple-quote format is like a full match report that spans multiple overs of text.

Syntax

swift
let name = "Ava"
let age = 28
let greeting = "Hello, \(name)! You are \(age) years old."

let multiLine = """
    Name: \(name)
    Age: \(age)
    """

Explanation

Inside a string literal, writing \( followed by an expression and a closing ) tells Swift to evaluate that expression and insert its textual representation into the string at that point. The expression can be a simple variable name, a more complex calculation, or even a function call, as long as the result can be represented as a String. Multi-line string literals are delimited by three double-quote characters (""") on their own lines; the content between them can span multiple lines and still supports the same \(...) interpolation syntax, which is convenient for formatted, multi-line templates.

🏏

Cricket analogy: Writing \(runs + balls) inside a string is like a scoreboard operator instantly calculating and inserting the strike rate mid-announcement, even from a full function call like calculateAverage(); a triple-quoted match summary spanning several overs still supports this same live insertion at any point.

Example

swift
let itemCount = 3
let unitPrice = 4.5
let summary = "You bought \(itemCount) items for a total of \(Double(itemCount) * unitPrice)"
print(summary)

let receipt = """
    Items: \(itemCount)
    Total: \(Double(itemCount) * unitPrice)
    """
print(receipt)

Output

swift
You bought 3 items for a total of 13.5
Items: 3
Total: 13.5

Key Takeaways

  • String interpolation embeds expressions inside a string literal using \(expression).
  • Interpolated expressions can be simple variables or more complex calculations.
  • Multi-line string literals are delimited by triple double-quotes (""").
  • Multi-line strings support the same \(...) interpolation syntax as single-line strings.
  • Interpolation is generally more readable than concatenating strings with the + operator.

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#StringInterpolationInSwift#String#Interpolation#Syntax#Explanation#StudyNotes#SkillVeris