0
0
Swiftprogramming~10 mins

String interpolation in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String interpolation
Start
Define variables
Create string with \(variable)
Swift replaces \(variable) with value
Final string ready
Print or use string
End
Swift replaces placeholders inside \( ) with variable values to build the final string.
Execution Sample
Swift
let name = "Anna"
let age = 30
let greeting = "Hello, \(name)! You are \(age) years old."
print(greeting)
This code creates a greeting string by inserting variables name and age inside the string.
Execution Table
StepActionExpression EvaluatedResult
1Define variable namename = "Anna"name = "Anna"
2Define variable ageage = 30age = 30
3Create greeting string with interpolation"Hello, \(name)! You are \(age) years old.""Hello, Anna! You are 30 years old."
4Print greetingprint(greeting)Output: Hello, Anna! You are 30 years old.
💡 All variables replaced and string printed, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
nameundefined"Anna""Anna""Anna""Anna"
ageundefinedundefined303030
greetingundefinedundefinedundefined"Hello, Anna! You are 30 years old.""Hello, Anna! You are 30 years old."
Key Moments - 2 Insights
Why does the string show the variable values instead of the variable names?
Because Swift replaces the \(variable) placeholders with the actual values during string creation, as shown in step 3 of the execution_table.
Can I put expressions inside \( ) or only variables?
You can put any valid Swift expression inside \( ), not just variables. Swift evaluates it and inserts the result, as the concept_flow shows the evaluation step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of greeting after step 3?
A"Hello, name! You are age years old."
B"Hello, Anna! You are 30 years old."
C"Hello, \(name)! You are \(age) years old."
D"Hello, Anna! You are age years old."
💡 Hint
Check the 'Result' column in row for step 3 in execution_table.
At which step does Swift replace the variables with their values inside the string?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table step 3.
If we change age to 31 before creating greeting, what will greeting contain?
A"Hello, Anna! You are age years old."
B"Hello, Anna! You are 30 years old."
C"Hello, Anna! You are 31 years old."
D"Hello, \(name)! You are \(age) years old."
💡 Hint
Refer to variable_tracker and how greeting uses current age value at creation.
Concept Snapshot
Swift String Interpolation:
Use \(variable) inside strings to insert variable values.
Supports any expression inside \( ).
Swift evaluates and replaces placeholders when string is created.
Example: "Hello, \(name)!" becomes "Hello, Anna!" if name = "Anna".
Full Transcript
This visual trace shows how Swift string interpolation works step-by-step. First, variables name and age are defined. Then a string greeting is created using \(name) and \(age) placeholders. Swift replaces these placeholders with the actual values "Anna" and 30 during string creation. Finally, printing greeting outputs the full message with values inserted. The variable tracker shows how each variable changes from undefined to their assigned values. Key moments clarify why placeholders become values and that expressions can be used inside \( ). The quiz tests understanding of when and how interpolation happens and what the final string contains.