Home »
Swift »
Swift Programs
Swift program to demonstrate the '\()' to embed the value of the variable within the string
Here, we are going to demonstrate the '\()' to embed the value of the variable within the string in Swift programming language.
Submitted by Nidhi, on May 29, 2021
Problem Solution:
Here, we will use '\()' to embed the value of the variable within the string and print the result on the console screen.
Program/Source Code:
The source code to demonstrate the '\()' to embed the value of the variable within the string is given below. The given program is compiled and executed successfully.
// Swift program to demonstrate the '\()' to embed
// the value of variables within the string
var num:Int = 10;
var pi:Float = 3.14;
var str:String="Pi : \(pi)";
print("Num: \(num)");
print(str);
Output:
Num: 10
Pi : 3.14
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we created two variables num, pi initialized with 10, 3.14 respectively. Then we created a string variable str, which is embedded the value of variable pi within the string initialized in the str variable. After that, we printed the result on the console screen.
Swift Basic Programs »