100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Kotlin

String Templates in Kotlin

Learn how Kotlin's built-in string interpolation with $variable and ${expression} simplifies building text.

BasicsBeginner6 min readJul 8, 2026
Analogies

Introduction

String templates are one of Kotlin's most convenient features, letting you embed variables and expressions directly inside string literals instead of concatenating with the + operator or using format specifiers. A template expression starts with a dollar sign ($) followed by a variable name, or with ${ } to wrap a more complex expression such as a function call or arithmetic operation.

🏏

Cricket analogy: Instead of splicing together 'Virat' + ' scored ' + 89 + ' runs', a commentator's teleprompter just drops $name and ${runs} straight into the sentence, and complex stats like ${strikeRate * 100} go inside braces too.

Syntax

kotlin
val name = "Ada"
val greeting = "Hello, $name"

val a = 4
val b = 5
val result = "Sum is ${a + b}"

val message = "Name length is ${name.length}"

Explanation

When you write "Hello, $name" Kotlin substitutes the current value of the name variable directly into the resulting string at runtime, no + concatenation required. For anything beyond a simple variable name — arithmetic, method calls, property access, or object member access — wrap the expression in curly braces: ${a + b} or ${name.length}. This is more readable than chains of string concatenation and avoids common formatting mistakes. Because templates are evaluated at runtime, they can reference any value in scope, including nullable variables (which render as the text 'null' if the value is null).

🏏

Cricket analogy: 'Hello, $name' becomes 'Hello, Sachin' automatically at runtime, but showing a computed strike rate needs braces like ${runs.toDouble() / balls * 100}; a nullable player.nickname would just print 'null' if unset.

Example

kotlin
fun main() {
    val user = "Priya"
    val itemsInCart = 3
    val pricePerItem = 12.5

    println("Welcome, $user!")
    println("You have $itemsInCart items in your cart.")
    println("Total cost: ${itemsInCart * pricePerItem}")
    println("Username in caps: ${user.uppercase()}")
}

Output

text
Welcome, Priya!
You have 3 items in your cart.
Total cost: 37.5
Username in caps: PRIYA

Key Takeaways

  • Use $variableName inside a string literal to insert a simple variable's value.
  • Use ${expression} for anything more complex, such as arithmetic, function calls, or property access.
  • String templates are evaluated at runtime and eliminate the need for + concatenation in most cases.
  • Templates work inside both regular double-quoted strings and triple-quoted raw strings.
  • A literal dollar sign in a string can be escaped as \$ when you don't want interpolation.

Practice what you learned

Was this page helpful?

Topics covered

#Kotlin#KotlinProgrammingStudyNotes#Programming#StringTemplatesInKotlin#String#Templates#Syntax#Explanation#StudyNotes#SkillVeris