Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use String Interpolation in Swift
Written by Team Kodeco

String interpolation allows you to include the value of variables and expressions in a string. This makes it easy to create dynamic strings.

Here’s how to use string interpolation in swift:

let name = "Bob"
let age = 30
print("My name is \(name) and I am \(age) years old.")
// Output: "My name is Bob and I am 30 years old."

You can also use string interpolation to include the result of expressions:

let a = 2
let b = 3
print("The result of 2 + 3 is \(a + b)")
// Output: "The result of 2 + 3 is 5

You can include any expression here, including function calls:

func add(a: Int, b: Int) -> Int {
    return a + b
}
print("The result of 2 + 3 is \(add(a: 2, b: 3))")
// Output: "The result of 2 + 3 is 5
© 2024 Kodeco Inc.