0
0
Swiftprogramming~10 mins

String interpolation in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"name"
Bname()
Cname
DName
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.
2fill in blank
medium

Complete 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'
Aa + b
B"a + b"
Csum(a, b)
Da.plus(b)
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).
3fill in blank
hard

Fix 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'
A(age)
Bage
C"age"
D\(age)
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the \( ) around the variable.
Putting the variable name in quotes.
4fill in blank
hard

Fill 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'
Atemp
Bunit
Ctemp + unit
D"temp"
Attempts:
3 left
💡 Hint
Common Mistakes
Putting variables in quotes inside interpolation.
Trying to combine variables inside one interpolation.
5fill in blank
hard

Fill 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'
A\(name)
B\(age)
C\(city)
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without \( ) inside the string.
Putting variable names in quotes.