Swift Academy · Lesson

Custom String Interpolation

Extend interpolation with your own segments.

Lesson 4 of 413 steps

Custom String Interpolation is a free Swift Academy lesson on CoddyKit. This is lesson 4 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.

Custom String Interpolation

Swift lets you extend how interpolation works by adding methods to String.StringInterpolation. This customizes what happens for specific argument types or labels.

extension String.StringInterpolation {
    mutating func appendInterpolation(shout value: String) {
        appendLiteral(value.uppercased() + "!")
    }
}
print("Message: \(shout: "hello")")

The appendInterpolation Method

Each custom interpolation is a mutating func appendInterpolation(...). Its parameters define what you write between the parentheses in a string.

extension String.StringInterpolation {
    mutating func appendInterpolation(repeat text: String, times: Int) {
        appendLiteral(String(repeating: text, count: times))
    }
}
print("\(repeat: "ab", times: 3)")

appendLiteral and appendInterpolation

Inside a custom method you build output by calling appendLiteral for plain text and reusing existing interpolations for values.

extension String.StringInterpolation {
    mutating func appendInterpolation(twice value: Int) {
        appendLiteral("\(value)\(value)")
    }
}
print("\(twice: 7)")

Labeled Interpolation Arguments

The first parameter label becomes the keyword you use, like \(shout: ...). This makes intent clear at the call site.

extension String.StringInterpolation {
    mutating func appendInterpolation(currency amount: Double) {
        appendLiteral(String(format: "$%.2f", amount))
    }
}
print("Price: \(currency: 9.5)")

Multiple Parameters

Custom interpolations can take several parameters, letting you pass options alongside the value.

extension String.StringInterpolation {
    mutating func appendInterpolation(_ value: Double, places: Int) {
        appendLiteral(String(format: "%.\(places)f", value))
    }
}
print("\(3.14159, places: 2)")

Formatting Dates of Values

A common use is presenting a value in a domain-specific way, such as padding a number to a fixed width.

extension String.StringInterpolation {
    mutating func appendInterpolation(padded value: Int, width: Int) {
        appendLiteral(String(format: "%0\(width)d", value))
    }
}
print("ID: \(padded: 42, width: 5)")

Conditional Output

Custom interpolation can include logic, producing different text based on the input.

extension String.StringInterpolation {
    mutating func appendInterpolation(yesNo flag: Bool) {
        appendLiteral(flag ? "Yes" : "No")
    }
}
print("Active: \(yesNo: true)")

Reusing Existing Interpolations

Within your method you can call appendInterpolation for built-in types, composing on top of standard behavior.

extension String.StringInterpolation {
    mutating func appendInterpolation(bracketed value: Int) {
        appendLiteral("[")
        appendInterpolation(value)
        appendLiteral("]")
    }
}
print("\(bracketed: 9)")

Default Parameter Values

Custom interpolations can give parameters defaults, so callers may omit them.

extension String.StringInterpolation {
    mutating func appendInterpolation(money value: Double, symbol: String = "$") {
        appendLiteral(symbol + String(format: "%.2f", value))
    }
}
let sym = "EUR "
print("\(money: 5.0)")
print("\(money: 5.0, symbol: sym))")

When to Use Custom Interpolation

Reach for custom interpolation when a formatting pattern recurs across your code; it centralizes the logic behind a clean call site.

extension String.StringInterpolation {
    mutating func appendInterpolation(stars n: Int) {
        appendLiteral(String(repeating: "*", count: n))
    }
}
print("Rating: \(stars: 4)")

Custom Interpolation Summary

By extending String.StringInterpolation, you turn repetitive formatting into readable, reusable syntax embedded right in your strings.

extension String.StringInterpolation {
    mutating func appendInterpolation(percent value: Double) {
        appendLiteral(String(format: "%.0f%%", value * 100))
    }
}
print("Done: \(percent: 0.75)")

Quick Check

Recall the mechanism behind custom interpolation.

Recap: Custom String Interpolation

You learned that extending String.StringInterpolation with appendInterpolation methods lets you define new interpolation syntax, using appendLiteral and nested interpolations to build output, with labels, multiple parameters, and defaults for clean reusable formatting.

extension String.StringInterpolation {
    mutating func appendInterpolation(upper value: String) {
        appendLiteral(value.uppercased())
    }
}
print("Hello \(upper: "swift")")
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 “Custom String Interpolation” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Custom 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 “Custom String Interpolation”?

Extend interpolation with your own segments. 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 4 of 4.

How long does the “Custom 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