Swift Academy · Lesson

Basic String Interpolation

Embed values directly inside string literals.

Lesson 1 of 413 steps

Basic String Interpolation is a free Swift Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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")
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Basic String Interpolation” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Basic String Interpolation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. Basic String Interpolation
  2. Formatting Numbers in Strings
  3. Multiline String Literals
  4. Custom String Interpolation
← Back to Swift Academy