String interpolation is a way to insert values or expressions directly into a string by placing them inside \( ) within the string.
Click to reveal answer
beginner
How do you insert a variable named name into a string using string interpolation?
Use \(name) inside the string. For example: "Hello, \(name)!" will insert the value of name.
Click to reveal answer
intermediate
Can you use expressions inside string interpolation? Give an example.
Yes! You can put any expression inside \( ). For example: "2 + 3 = \(2 + 3)" will show 2 + 3 = 5.
Click to reveal answer
intermediate
What happens if you try to use string interpolation outside of a string literal?
String interpolation only works inside string literals. Using \( ) outside strings will cause a syntax error.
Click to reveal answer
beginner
Why is string interpolation useful compared to string concatenation?
String interpolation is easier to read and write. It avoids many + signs and automatically converts values to strings.
Click to reveal answer
How do you insert the value of a variable age into a string using Swift string interpolation?
A"I am + age + years old"
B"I am \(age) years old"
C"I am ${age} years old"
D"I am age years old"
✗ Incorrect
In Swift, string interpolation uses \(variable) inside a string literal.
Which of these is a valid string interpolation expression in Swift?
A"Sum is \(5 + 10)"
B"Sum is ${5 + 10}"
C"Sum is \{5 + 10}"
D"Sum is (5 + 10)"
✗ Incorrect
Swift uses \(expression) inside strings for interpolation.
What will this Swift code print? let name = "Anna" print("Hello, \(name)!")
AHello, \(name)!
BError
CHello, name!
DHello, Anna!
✗ Incorrect
The \(name) is replaced by the value of the variable name.
Can you use string interpolation to call a function inside a string in Swift?
AOnly if the function returns a string
BNo, functions cannot be used inside strings
CYes, by placing the function call inside \( )
DOnly if the function is declared inside the string
✗ Incorrect
Any expression, including function calls, can be used inside \( ) in Swift strings.
What is the output of this code? print("3 * 4 = \(3 * 4)")
A3 * 4 = 12
B3 * 4 = \(3 * 4)
C3 * 4 = 3 * 4
DError
✗ Incorrect
The expression 3 * 4 is evaluated and inserted into the string.
Explain how string interpolation works in Swift and why it is useful.
Think about how you put values inside strings easily.
You got /4 concepts.
Write a Swift string that uses interpolation to show the result of 10 divided by 2.
Use \( ) inside quotes with the division expression.
You got /3 concepts.
Practice
(1/5)
1. What does string interpolation in Swift allow you to do?
easy
A. Convert a string to uppercase or lowercase
B. Create a new string by repeating characters multiple times
C. Split a string into an array of characters
D. Insert variables or expressions directly inside a string using \( )
Solution
Step 1: Understand string interpolation purpose
String interpolation lets you put variables or expressions inside a string easily.
Step 2: Recognize the syntax
Using \( ) inside double quotes inserts the value dynamically.
Final Answer:
Insert variables or expressions directly inside a string using \( ) -> Option D
Quick Check:
String interpolation = Insert variables with \( ) [OK]
Hint: Remember: \( ) inside quotes inserts values fast [OK]
Common Mistakes:
Confusing interpolation with string repetition
Trying to use single quotes instead of double quotes
Forgetting the backslash before parentheses
2. Which of the following is the correct way to use string interpolation in Swift to show the value of variable score inside a string?
easy
A. "Your score is: ${score}"
B. "Your score is: score"
C. "Your score is: \(score)"
D. "Your score is: {score}"
Solution
Step 1: Identify correct interpolation syntax
Swift uses \(variable) inside double quotes for interpolation.
Step 2: Check each option
"Your score is: \(score)" uses \(score), which is correct. Others use wrong syntax.
Final Answer:
"Your score is: \(score)" -> Option C
Quick Check:
Swift interpolation = \(variable) [OK]
Hint: Use backslash and parentheses for variables inside strings [OK]
Common Mistakes:
Using ${} like in other languages
Forgetting the backslash before parentheses
Not using double quotes around the string
3. What will be the output of the following Swift code?
let name = "Anna"
let age = 30
print("\(name) is \(age) years old.")
medium
A. Anna is 30 years old.
B. name is age years old.
C. \(name) is \(age) years old.
D. Anna is \(age) years old.
Solution
Step 1: Understand variable values
name = "Anna", age = 30.
Step 2: Apply string interpolation
"\(name)" becomes "Anna", "\(age)" becomes "30" in the string.
Final Answer:
Anna is 30 years old. -> Option A
Quick Check:
Interpolation replaces variables with values [OK]
Hint: Replace \(variable) with its value inside the string [OK]
Common Mistakes:
Printing the literal \(name) instead of value
Confusing variable names with strings
Missing backslash causing no interpolation
4. Identify the error in this Swift code snippet using string interpolation:
let temperature = 22
print("The temperature is (temperature) degrees.")
medium
A. Missing backslash before parentheses in interpolation
B. Variable temperature is not declared
C. Using single quotes instead of double quotes
D. Parentheses should be square brackets
Solution
Step 1: Check interpolation syntax
Interpolation requires a backslash before parentheses: \(variable).
Step 2: Identify missing backslash
Code uses (temperature) without backslash, so it won't interpolate.
Final Answer:
Missing backslash before parentheses in interpolation -> Option A
Quick Check:
Backslash is required for interpolation [OK]
Hint: Always add \ before (variable) in strings [OK]
Common Mistakes:
Forgetting the backslash
Using wrong quote types
Assuming parentheses alone interpolate
5. You want to create a message that shows the total price including tax. Given price = 50 and taxRate = 0.1, which Swift code correctly uses string interpolation to print "Total price: 55.0"?