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
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
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
Welcome, Priya!
You have 3 items in your cart.
Total cost: 37.5
Username in caps: PRIYAKey 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
1. What is the correct syntax to insert the value of a variable named count into a string?
2. How do you embed the result of an expression like a + b inside a string template?
3. What does "Length: ${name.length}" produce if name is "Kotlin"?
4. How do you include a literal dollar sign character in a Kotlin string without triggering interpolation?
5. What is a key benefit of string templates over manual concatenation with +?
Was this page helpful?
You May Also Like
Variables and Data Types in Kotlin
Learn how Kotlin declares variables with val and var, and the built-in data types used to store values.
Type Conversion in Kotlin
Understand why Kotlin requires explicit conversion between numeric types and how to use functions like toInt() and toDouble().
Input and Output in Kotlin
Learn how to print output with println()/print() and read user input with readLine() in Kotlin.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics