0
0
Kotlinprogramming~10 mins

String templates and interpolation in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String templates and interpolation
Start
Define variables
Create string with $variable
Evaluate expression inside ${}
Replace placeholders with values
Final string ready
Print or use string
End
This flow shows how Kotlin replaces variables and expressions inside strings with their actual values to create the final string.
Execution Sample
Kotlin
val name = "Anna"
val age = 25
val greeting = "Hello, $name! You are ${age + 1} years old."
println(greeting)
This code creates a greeting string using variables and an expression inside the string, then prints it.
Execution Table
StepActionEvaluationResult
1Define variable namename = "Anna"name = "Anna"
2Define variable ageage = 25age = 25
3Create string with templates"Hello, $name! You are ${age + 1} years old."Placeholders found: $name and ${age + 1}
4Replace $name with value$name -> "Anna""Hello, Anna! You are ${age + 1} years old."
5Evaluate expression ${age + 1}age + 1 = 26"Hello, Anna! You are 26 years old."
6Print final stringprintln(...)Output: Hello, Anna! You are 26 years old.
7EndNo more actionsExecution complete
💡 All placeholders replaced and string printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
nameundefined"Anna""Anna""Anna"
ageundefinedundefined2525
greetingundefinedundefinedundefined"Hello, Anna! You are 26 years old."
Key Moments - 2 Insights
Why do we use ${} inside the string instead of just $ for expressions?
The ${} syntax tells Kotlin to evaluate the expression inside it, like age + 1, while $ alone is only for simple variable names. See execution_table rows 3, 4, and 5.
What happens if we forget to use $ before a variable inside the string?
Without $, Kotlin treats it as normal text, not a variable. So the variable name won't be replaced. This is shown in execution_table row 3 where placeholders are detected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of greeting after step 5?
A"Hello, Anna! You are 26 years old."
B"Hello, $name! You are ${age + 1} years old."
C"Hello, Anna! You are ${age + 1} years old."
D"Hello, $name! You are 26 years old."
💡 Hint
Check execution_table row 5 where expression is evaluated and placeholders replaced.
At which step does Kotlin replace the $name placeholder with its value?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look at execution_table row 4 where $name is replaced.
If age was 30 instead of 25, what would be the output after step 6?
AHello, Anna! You are 25 years old.
BHello, Anna! You are 30 years old.
CHello, Anna! You are 31 years old.
DHello, Anna! You are 26 years old.
💡 Hint
Check how expression ${age + 1} is evaluated in execution_table row 5.
Concept Snapshot
String templates use $variable or ${expression} inside strings.
Kotlin replaces these with actual values when creating the string.
Use $ for simple variables, ${} for expressions.
This makes string building easy and readable.
Example: "Hello, $name! You are ${age + 1} years old."
Full Transcript
This visual trace shows how Kotlin handles string templates and interpolation. First, variables name and age are defined. Then a string greeting is created with placeholders $name and ${age + 1}. Kotlin replaces $name with "Anna" and evaluates the expression age + 1 to 26, replacing ${age + 1}. Finally, the complete string "Hello, Anna! You are 26 years old." is printed. This process helps build strings easily by embedding variables and expressions directly inside them.