String Templates (Kotlin)

String templates embed variables and expressions directly inside strings using $name for a variable and ${expression} for anything more complex, avoiding manual concatenation.

Kotlin

Inside a normal or triple-quoted string, $variable is replaced by the variable's value, and ${...} evaluates any expression — a method call, arithmetic, or property access. This is Kotlin's equivalent of Python f-strings or JavaScript template literals.

Use the simple $name form for plain identifiers and the braced ${...} form when you need an expression or to disambiguate (e.g. ${user.name} or ${list.size}). To include a literal dollar sign, escape it as \$.

Triple-quoted raw strings combine well with templates for multi-line text, and .trimIndent() cleans up leading whitespace. String templates make building output and log messages clean and readable.

Syntax

"Hello, $name! You are ${age + 1} next year."

Examples

Kotlin

val name = "Sam"
val items = listOf(1, 2, 3)
println("$name has ${items.size} items")

Output:

Sam has 3 items

Learn it in a lesson

See also

Back to the Dictionary