0Pricing
Kotlin Academy · Lesson

String Templates: $variable and ${expression}

Embed variables and expressions inside strings using template syntax.

String Templates: $variable and ${expression} is a free Kotlin Academy lesson on CoddyKit — lesson 2 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 Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are String Templates?

String templates let you embed values directly into a string using $ for simple variables and ${...} for full expressions.

Embedding a Variable

Prefix a variable name with $ inside a string. Kotlin substitutes its value at runtime.

fun main() {
    val name = "Ada"
    println("Hello, $name!") // Hello, Ada!
}

Multiple Variables

Embed as many variables as you need. Each $name is replaced independently.

fun main() {
    val first = "Marie"
    val last = "Curie"
    val year = 1903
    println("$first $last won Nobel in $year")
}

Expressions with ${...}

For anything beyond a single identifier — arithmetic, method calls, property access — use ${expression}.

fun main() {
    val a = 10
    val b = 5
    println("Sum: ${a + b}")
    println("Product: ${a * b}")
}

Calling Methods Inside Templates

Templates can invoke functions and properties. Anything that evaluates to a value works.

fun main() {
    val word = "kotlin"
    println("Length: ${word.length}")
    println("Upper: ${word.uppercase()}")
}

Conditional Expressions

Kotlin if is an expression, so you can embed it directly in a template.

fun main() {
    val score = 75
    println("Result: ${if (score >= 60) "pass" else "fail"}")
}

Escaping the Dollar Sign

If you need a literal $, escape it with a backslash: \$.

fun main() {
    val price = 19
    println("Cost: \$$price") // Cost: $19
}

Nested Templates

You can nest string templates inside ${...}, though deeply nested strings get hard to read.

fun main() {
    val items = listOf("apple", "banana")
    println("List: ${items.joinToString { "[$it]" }}")
}

Templates with Collections

Combine templates with collection operations to format output succinctly.

fun main() {
    val numbers = listOf(1, 2, 3, 4, 5)
    println("Sum of ${numbers.size} numbers: ${numbers.sum()}")
}

Templates and Type Conversion

Any type with a sensible toString() works inside templates — booleans, numbers, custom classes.

data class Point(val x: Int, val y: Int)
fun main() {
    val p = Point(3, 4)
    println("Point: $p") // uses Point.toString()
    println("X coord: ${p.x}")
}

Performance Note

The Kotlin compiler converts string templates to StringBuilder calls, similar to manual concatenation — usually no performance difference. Prefer templates for readability.

fun main() {
    val n = 42
    val a = "Value is $n"       // template
    val b = "Value is " + n      // concatenation
    println(a == b) // true
}

Quick Check

Which template syntax correctly embeds the result of x + 1?

Recap

Use $variable for simple identifiers and ${expression} for anything more complex. Escape literal dollars with \$. Templates compile to efficient StringBuilder code.

Frequently asked questions

Is the “String Templates: $variable and ${expression}” lesson free?

Yes — the full text of “String Templates: $variable and ${expression}” is free to read here on the web, and the Kotlin 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 Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “String Templates: $variable and ${expression}”?

Embed variables and expressions inside strings using template syntax. You practise Kotlin 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 Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “String Templates: $variable and ${expression}” 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 Kotlin Academy lesson?

Yes. Every Kotlin 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

  1. String Basics: Literals, Escape Sequences & Length
  2. String Templates: $variable and ${expression}
  3. Raw Strings and Triple Quotes
  4. Key String Functions: trim, split, replace, contains
← Back to Kotlin Academy