Skip to main content
St Louis

Back to all posts

How to Perform String Interpolation In Kotlin?

Published on
4 min read
How to Perform String Interpolation In Kotlin? image

Best Kotlin Programming Tools to Buy in October 2025

1 Thriving in Android Development Using Kotlin: A project-based guide to using the latest Android features for developing production-grade apps

Thriving in Android Development Using Kotlin: A project-based guide to using the latest Android features for developing production-grade apps

BUY & SAVE
$38.49 $44.99
Save 14%
Thriving in Android Development Using Kotlin: A project-based guide to using the latest Android features for developing production-grade apps
2 Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin

BUY & SAVE
$59.30 $89.99
Save 34%
Head First Android Development: A Learner's Guide to Building Android Apps with Kotlin
3 Android UI Development with Jetpack Compose: Bring declarative and native UI to life quickly and easily on Android using Jetpack Compose and Kotlin

Android UI Development with Jetpack Compose: Bring declarative and native UI to life quickly and easily on Android using Jetpack Compose and Kotlin

BUY & SAVE
$36.26
Android UI Development with Jetpack Compose: Bring declarative and native UI to life quickly and easily on Android using Jetpack Compose and Kotlin
4 Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose

BUY & SAVE
$36.99 $49.99
Save 26%
Modern Android 13 Development Cookbook: Over 70 recipes to solve Android development issues and create better apps with Kotlin and Jetpack Compose
5 Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)

BUY & SAVE
$50.80
Kotlin Programming: The Big Nerd Ranch Guide (Big Nerd Ranch Guides)
6 Learn Android Studio 3 with Kotlin: Efficient Android App Development

Learn Android Studio 3 with Kotlin: Efficient Android App Development

BUY & SAVE
$43.03
Learn Android Studio 3 with Kotlin: Efficient Android App Development
+
ONE MORE?

String interpolation in Kotlin allows you to embed expressions inside a string. This helps create dynamic strings by evaluating and substituting the expressions with their values. To perform string interpolation in Kotlin, you can utilize the following options:

  1. Using the $ character: You can use the $ character followed by the expression to interpolate it directly into the string. For example: val name = "John" val age = 25 val message = "My name is $name and I'm $age years old."
  2. Using the ${} syntax: For more complex expressions or accessing object properties, you can enclose the expression inside ${}. For example: val person = Person("Jane", 30) val message = "My name is ${person.name} and I'm ${person.age} years old."
  3. Escaping the $ character: If you need to use the $ character itself without interpolation, you can escape it by using $$. For example: val price = 10 val message = "The price is $$price."

String interpolation in Kotlin provides a concise and readable way to construct dynamic strings based on variables and expressions.

What is the function of the $ symbol in string interpolation in Kotlin?

In Kotlin, the $ symbol is used in string interpolation to embed variables or expressions directly into a string. By placing a $ before the variable or expression within a double-quoted string, it will be replaced with its value during runtime. Here's an example:

val name = "John" val age = 30

val message = "My name is $name and I am $age years old." println(message)

Output:

My name is John and I am 30 years old.

In this example, the $name is replaced with the value of the name variable (which is "John") and the $age is replaced with the value of the age variable (which is 30). This makes it easy to include variables or expressions within a string without having to concatenate them using the + operator.

How to handle null values in string interpolation in Kotlin?

In Kotlin, you can handle null values in string interpolation by using the safe call operator ?. and the Elvis operator ?:.

With the safe call operator ?., you can access properties or call methods on nullable objects without causing a null pointer exception. If the object is null, the result of the expression will be null too.

The Elvis operator ?: allows you to provide a default value in case of a null value. If the expression on the left side of the Elvis operator is null, the expression on the right side will be used as a fallback.

Here's an example that demonstrates how to handle null values in string interpolation:

val nullableValue: String? = null val nonNullableValue: String = "Hello"

val combinedValue = "The value is: ${nullableValue?.length ?: "unknown"}" println(combinedValue) // Output: "The value is: unknown"

val anotherValue = "The value is: ${nonNullableValue?.length ?: "unknown"}" println(anotherValue) // Output: "The value is: 5" (length of "Hello" is 5)

In the above example, if nullableValue is null, the length will be replaced with "unknown" using the Elvis operator. On the other hand, nonNullableValue is not null, so its length will be used.

By using the safe call operator ?. and the Elvis operator ?:, you can handle null values in string interpolation effectively in Kotlin.

How to perform nested string interpolation in Kotlin?

In Kotlin, nested string interpolation can be performed by utilizing the $ character inside a string enclosed by double quotes ("). Follow the steps below to perform nested string interpolation:

  1. Create a string with the outer and inner interpolated values enclosed by double quotes.
  2. Use the $ character followed by the inner interpolated value inside the outer interpolated value.
  3. Enclose the entire string within another set of double quotes.

Here's an example:

val outerValue = "Hello" val innerValue = "World"

val message = "$outerValue, \${$innerValue}!" println(message)

Output:

Hello, ${World}!

By adding a backslash (\) before the $ character, you escape the inner interpolation, preventing it from being evaluated.

What is the performance impact of string interpolation in Kotlin?

String interpolation in Kotlin has a negligible performance impact because it is optimized by the Kotlin compiler.

During compilation, the compiler transforms string interpolation expressions into concatenation operations using a StringBuilder. This optimization reduces the runtime overhead and ensures that string interpolation has comparable performance to traditional concatenation.

The resulting compiled code for string interpolation is efficient and performs well, even with complex expressions or multiple interpolations within a single string. Therefore, developers can freely use string interpolation in Kotlin without significant performance concerns.