Swift Program to demonstrate the string interpolation


In Swift, String interpolation is a great feature to create a new string by embedding the value of a variable, constant, function and literal directly in the given string literal. Or we can say that using string interpolation we can create a string by combining static text and dynamic values.

Syntax

var myString = “hello! \(x). How are you?”

We can perform string interpolation by wrapping a string literal or constant or variable in a pair of parentheses that is prefixed by a backslash(\) for example, \(x).

Example 1

In the following Swift program, we will demonstrate how to perform string interpolation. So first we create the variables with values. Then using string interpolation we insert the variable inside the string literal using “\(varName)”. The values of the variables will automatically be converted into a string and will be inserted in the specified string.

import Foundation
import Glibc

let num1 = 54
let num2 = 10
let res = num1*num2

print("The product of \(num1) and \(num2) is \(res)")

let name = "Sumonika"
let age = 23

print("Hey my name is \(name) and my age is \(age)")

Output

The product of 54 and 10 is 540
Hey my name is Sumonika and my age is 23

Example 2

In the following Swift program, we will demonstrate how to perform string interpolation. So first we create the variables with values. Then using string interpolation we insert the variable inside the multiline string literal using “\(varName)”. Where the values of the variables will automatically be converted into a string and inserted in the resultant string.

import Foundation
import Glibc

let num1 = 54
let num2 = 10
let res = num1+num2

print("""
      Addition:
      \(num1) + \(num2) = \(res)
      """)

let name = "Novika"
let age = 23
let city = "Delhi"

print("""
      \nHey my name is \(name) 
      my age is \(age)
      I lived in \(city)
      """)

Output

Addition:
54 + 10 = 64

Hey my name is Novika 
my age is 23
I lived in Delhi

Example 3

In the following Swift program, we will demonstrate how to perform string interpolation on arithmetic operation. So first we create the variables with values. Then using string interpolation we insert the variable inside the string literal using “\(varName)”. Where the arithmetic operation defined in the string interpolation will perform their task and display the result.

import Foundation
import Glibc

let m = 19
let n = 100

let sum = "\(m) + \(n) = \(m + n)"
let difference = "\(n) - \(m) = \(n - m)"
let product = "\(n) * \(m) = \(n * m)"
let quotient = "\(m) / \(n) = \(Double(m) / Double(n))"

print("Sum:", sum)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)

Output

Sum: 19 + 100 = 119
Difference: 100 - 19 = 81
Product: 100 * 19 = 1900
Quotient: 19 / 100 = 0.19

Example 4

In the following Swift program, we will demonstrate how to perform boolean value interpolation. So first we create the variables of boolean type with values. Then using string interpolation we insert the boolean values in the interpolated strings. The boolean values will automatically converts into a string and will be inserted in the specified string at the time of interpolation.

import Foundation
import Glibc

let isCow = true
let isGoat = false

let animal1 = "Is it cow?: \(isCow)"
let animal2 = "Is it goat?: \(isGoat)"

print(animal1)
print(animal2)

Output

Is it cow?: true
Is it goat?: false

Conclusion

So this is how we can perform string interpolation. You can use String interpolation to display variable values, create URLs, interpolate values from function calls, etc. String interpolation can be performed on a single line or multiline string literals.

Updated on: 13-Jun-2023

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements