Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the value of the variable inside the string.
Swift
let name = "Alice" print("Hello, \([1])!")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the variable name in quotes inside the interpolation.
Using parentheses after the variable name like a function call.
✗ Incorrect
In Swift, to insert a variable's value inside a string, use \(variableName). Here, \(name) inserts the value of 'name'.
2fill in blank
mediumComplete the code to include the sum of two numbers inside the string.
Swift
let a = 5 let b = 3 print("Sum is \([1])")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the expression inside quotes, which prints the expression as text.
Using a function that doesn't exist like sum(a, b).
✗ Incorrect
Inside string interpolation, you can write expressions like 'a + b' to calculate and insert the result.
3fill in blank
hardFix the error in the string interpolation to correctly show the age.
Swift
let age = 30 print("Age: [1]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the \( ) around the variable.
Putting the variable name in quotes.
✗ Incorrect
To insert a variable's value inside a string in Swift, you must use \(variable). Just writing 'age' will print the word, not the value.
4fill in blank
hardFill both blanks to create a string that shows the temperature with a degree symbol.
Swift
let temp = 22 let unit = "C" let message = "Temperature: \([1])\([2])°"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting variables in quotes inside interpolation.
Trying to combine variables inside one interpolation.
✗ Incorrect
Use \(temp) and \(unit) to insert the temperature value and unit inside the string.
5fill in blank
hardFill all three blanks to create a greeting with name, age, and city using string interpolation.
Swift
let name = "Bob" let age = 25 let city = "Paris" let greeting = "Hi, I am [1], [2] years old, from [3]."
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without \( ) inside the string.
Putting variable names in quotes.
✗ Incorrect
Each blank must have the variable inside \( ) to insert its value into the string.