Custom String Interpolation
Extend interpolation with your own segments.
Custom String Interpolation is a free Swift Academy lesson on CoddyKit — lesson 4 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.
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")")Frequently asked questions
Is the “Custom String Interpolation” lesson free?
Yes — the full text of “Custom 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 “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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
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
- Basic String Interpolation
- Formatting Numbers in Strings
- Multiline String Literals
- Custom String Interpolation