Skip to main content
Browse topics

String Templates in Kotlin

2 min read Updated June 30, 2026
Share

Introduction

String Templates is a fundamental concept every Kotlin developer should understand. Strings appear in almost every program — user input, file paths, API responses, and UI labels. Kotlin’s standard library provides concise helpers for common text tasks.

String templates embed variables and expressions inside strings. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • String templates embed variables and expressions inside strings.
  • Use $variable for simple values and ${expression} for expressions.
  • Templates make output formatting simple and readable.

Syntax

kotlin
println("Value is $num")
println("Sum is ${a + b}")

String Templates in Kotlin Example Program in Kotlin

kotlin
fun main(args: Array<String>) {
    val name = "Kotlin"
    val version = 2
    println("Language: $name")
    println("Version: ${version + 0}")
}

Sample Output

plaintext
Language: Kotlin
Version: 2

When to use

Use string functions when formatting output, validating user input, splitting CSV data, or building URLs and file paths.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val name = "Kotlin" assigns or updates a value used later in the program.

  3. val version = 2 assigns or updates a value used later in the program.

  4. The println("Language: $name") statement writes a line to the console — this produces part of the sample output below.

  5. The println("Version: ${version + 0}") statement writes a line to the console — this produces part of the sample output below.

  6. String templates embed variables and expressions inside strings.

  7. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

Common Mistakes

Key Points

  • String templates embed variables and expressions inside strings.
  • Use $variable for simple values and ${expression} for expressions.
  • Templates make output formatting simple and readable.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is String Templates in Kotlin?
String templates embed variables and expressions inside strings.
When should I use String Templates?
Use string functions when formatting output, validating user input, splitting CSV data, or building URLs and file paths.
How is String Templates different from Java?
Templates make output formatting simple and readable.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related tutorials

Search tutorials