Build a formatted string with embedded expressions using backtick interpolation.

← Operators and Expressions · Ref: Q1164

Backtick strings with ${expr} for interpolation:

  stdout.println(`Name: ${name}, Age: ${age}`)
  stdout.println(`Total: ${price * quantity}`)

$ inside backticks is the interpolation marker, not the $ string operator. Double quotes never interpolate: "Name: ${name}" is a compile error (E07901). See Q909, Q918, Q43.

Example

defines module qa.operators.interpolationpatterns

  defines program

    InterpolationPatternsDemo()
      stdout <- Stdout()

      name <- "Alice"
      age <- 30
      price <- 9.99
      quantity <- 3

      //Basic interpolation
      stdout.println(`Name: ${name}, Age: ${age}`)

      //Arithmetic inside ${}
      stdout.println(`Total: ${price * quantity}`)

      //Multiple expressions
      stdout.println(`${name} is ${age} years old`)

Common mistakes

E11068 — EK9 forbids 3+ part string concatenation with '+'; use backtick interpolation instead. See ek9 -h E11068 for details.

Incorrect:

stdout.println("Name is " + name + " today")

Correct:

stdout.println(`Name: ${name}, Age: ${age}`)

E07901 — Only a backtick String interpolates; a double-quoted String holding ${ is a compile error (E07901) instead of printing the characters. See ek9 -h E07901 for details.

Incorrect:

stdout.println("Name: ${name}, Age: ${age}")

Correct:

stdout.println(`Name: ${name}, Age: ${age}`)
Other ways to ask this
  • I need to embed variable values and arithmetic inside a string
  • In JavaScript I'd use template literals with ${}. Write the EK9 interpolation
  • Given name, age, and a calculation, build a formatted output string
  • Construct a display string with multiple embedded expressions

Coming from another language?

JavaScript: `Hello ${name}`. Kotlin: "Hello $name". Python: f"Hello {name}". EK9: `Hello ${name}` — same as JavaScript. Kotlin, Groovy, Dart, PHP and the shells interpolate DOUBLE quotes; carried into EK9, "Hello ${name}" is a compile error (E07901).

Keywords: interpolation, backtick, template, format, string, embed