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
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
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
You bought 3 items for a total of 13.5
Items: 3
Total: 13.5Key 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
1. What syntax does Swift use to embed a variable inside a string literal?
2. What delimiter is used for a multi-line string literal in Swift?
3. Can multi-line string literals in Swift use interpolation?
4. What does the expression "Total: \(3 + 4)" evaluate to?
Was this page helpful?
You May Also Like
Type Inference and Conversion in Swift
Learn how Swift infers types from literal values and why converting between types always requires an explicit initializer call.
Data Types in Swift
Explore Swift's core data types—Int, Double, Float, Bool, String, Character, and tuples—and how Swift handles numeric type safety.
Variables and Constants in Swift
Learn how Swift's var and let keywords declare mutable variables and immutable constants, and why let is preferred by default.
Operators in Swift
Understand Swift's arithmetic, comparison, logical, and range operators, and how the closed and half-open range operators differ.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics