Basic String Interpolation
Embed values directly inside string literals.
Basic String Interpolation is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is String Interpolation
String interpolation builds a string by embedding values directly inside a string literal. You write a backslash followed by the value in parentheses, and Swift inserts its textual form.
let name = "Alice"
let greeting = "Hello, \(name)!"
print(greeting)Embedding a Number
Any value, not just strings, can be interpolated. Swift converts it to text automatically.
let age = 30
print("You are \(age) years old.")Interpolating Expressions
You can embed a whole expression inside the parentheses. Swift evaluates it first, then inserts the result.
let a = 7
let b = 3
print("Sum is \(a + b)")Multiple Interpolations
A single string may contain several interpolations, mixing literal text and dynamic values freely.
let item = "apple"
let count = 4
print("I have \(count) \(item)s")Interpolating Booleans
Boolean values interpolate as the words true or false.
let isReady = true
print("Ready: \(isReady)")Calling Methods Inside Interpolation
You can call functions or methods within the parentheses; their return value is inserted.
let word = "swift"
print("Uppercased: \(word.uppercased())")Interpolating Computed Values
Interpolation is great for reports: compute a value inline and present it in a sentence.
let price = 19.99
let quantity = 3
print("Total: \(price * Double(quantity))")Interpolation with Collections
Arrays and dictionaries interpolate using their debug-style text, useful for quick inspection.
let scores = [10, 20, 30]
print("Scores: \(scores)")Optionals in Interpolation
Interpolating an optional shows its wrapped form. Unwrap or use a default to print clean text.
let nickname: String? = "Ace"
print("Nickname: \(nickname ?? "none")")Building Strings Over Time
You can combine interpolation with string concatenation or accumulation to assemble longer output.
var report = ""
for i in 1...3 {
report += "Line \(i)\n"
}
print(report)Interpolation vs Concatenation
Interpolation is usually cleaner than gluing strings with +, especially when mixing types, because no manual conversion is needed.
let user = "Bea"
let visits = 5
print("\(user) visited \(visits) times")Quick Check
Recall the interpolation syntax.
Recap: Basic String Interpolation
You learned that interpolation embeds values and expressions inside string literals using the backslash-parentheses syntax, that any type is converted to text automatically, and that it is cleaner than manual concatenation for mixed-type output.
let user = "Cy"
let items = 2
print("\(user) bought \(items) items for \(items * 5) dollars")Frequently asked questions
Is the “Basic String Interpolation” lesson free?
Yes — the full text of “Basic String Interpolation” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Basic String Interpolation”?
Embed values directly inside string literals. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic String Interpolation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Basic String Interpolation
- Formatting Numbers in Strings
- Multiline String Literals
- Custom String Interpolation