String interpolation in Swift
Backslash-parens splice values; triple quotes span lines.
let skipper = "Rae"
let knots = 12.75
// \() interpolates any expression into a string literal.
print("\(skipper) reports \(knots) kts")
print("Rounded: \(Int(knots.rounded())) kts")
// Multiline literals keep their line breaks and indentation rules.
let notice = """
RACE NOTICE
Skipper: \(skipper)
First gun at 10:00
"""
print(notice)
How it works
\(skipper)drops a value into a literal.- Expressions work too:
\(Int(knots.rounded())). - A
"""literal keeps line breaks; closing-quote position sets indent.
Keywords and builtins used here
Intletprint
The run, in numbers
- Lines
- 14
- Characters to type
- 338
- Tokens
- 59
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 51 seconds.
Step 3 of 3 in Values, step 3 of 27 in Language basics.