typestar

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

  1. \(skipper) drops a value into a literal.
  2. Expressions work too: \(Int(knots.rounded())).
  3. A """ literal keeps line breaks; closing-quote position sets indent.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Values, step 3 of 27 in Language basics.

← Previous Next →

String interpolation in other languages