Beginner 6 min readKotlin 2.0
Kotlin String Templates โ $ and ${} Explained
String templates are Kotlin's way to embed values inside strings without concatenation. They make string formatting clean and readable.
What You Will Learn
- Use $ to embed variables
- Use ${} to embed expressions
- Escape $ with a backslash
- Use string templates in multi-line strings
Basic String Templates with $
Put $ before a variable name to embed its value inside a string.
Variable in String Template
kotlin
fun main() {
val name = "Kotlin"
val version = 2
println("Learning $name version $version")
println("$name is awesome!")
}Output
Learning Kotlin version 2
Kotlin is awesome!
The $ prefix inserts the value of the variable at that position. No concatenation needed.
Beginner Tip: If the variable name is immediately followed by a letter or digit that could be part of the name, use ${} to make the boundary clear: "${name}Guide".
Expressions with ${}
Use ${} to embed any Kotlin expression โ not just variable names.
Expressions in String Templates
kotlin
fun main() {
val a = 10
val b = 3
println("$a + $b = ${a + b}")
println("$a * $b = ${a * b}")
println("Uppercase: ${"kotlin".uppercase()}")
println("Name length: ${"Juned".length}")
}Output
10 + 3 = 13
10 * 3 = 30
Uppercase: KOTLIN
Name length: 5
${a + b} evaluates the expression and inserts the result. Any valid Kotlin expression can go inside ${}.
Escaping the $ Sign
To print a literal $ inside a string, use a backslash before it.
Printing a Dollar Sign
kotlin
fun main() {
val price = 99
println("Price: \$$price")
println("Save \$10 today!")
}Output
Price: $99
Save $10 today!
\$ prints a literal dollar sign. $price inserts the variable value after the literal dollar sign.
Practice Exercise
Exercisepredict output
What does this print? val x = 4 val y = 5 println("${x * y} = $x * $y")
Quick Quiz
Quick Quiz
When do you use ${} instead of $ in a string template?
Frequently Asked Questions
Related Tutorials
Last updated: 2026-05-01Kotlin 2.0
Written by KotlinGuide Editorial Team ยท Reviewed by KotlinGuide Technical Review