Swift Print Variables
Swift Print Variables
Print values with concatenation or string interpolation.
Use \(value) to insert values. You can set a separator and a terminator.
Concatenation and Interpolation
Use concatenation to combine strings and interpolate to embed values inside strings.
Example
let first = "Hello"
let second = "Swift"
// Concatenation
print(first + ", " + second)
// Interpolation
print("\(first), \(second)")
let a = 2, b = 3
print("a = \(a), b = \(b), sum = \(a + b)")
This example shows concatenation and interpolation.
Custom Separator and Terminator
Set a separator between items and a terminator at the end.
Example
let a = 1, b = 2, c = 3
print(a, b, c, separator: ", ", terminator: "; ")
print("done") // prints on the same line after a semicolon
This example sets a comma-space separator and a semicolon terminator.
Tip: Interpolation is safer and clearer than concatenation, especially with non-string values.