In Swift, there are times when we need to print the values of a variable in a string or sequence of characters. This can be achieved by using \()
. This enables us to easily print the value of a variable inside a string.
\(variable)
variable
: This is the variable whose value we want to embed inside a string.
The value returned is the value of the variable, variable
.
// create some variablesvar age:Int = 200;var pi:Float = 3.14;var role:String = "Developer";var name:String = "Theodore";// embed and print values of variablesprint("Hi! My name is \(name). I am a \(role).");print("Did you know I am \(age) years old!");
\()
.