Swift – Include Value of a Variable in String

To include value of a variable in a String in Swift, specify the variable in parentheses preceded by a back slash character.

The syntax to include value of a variable in a String is

</>
Copy
"Value is \(x)"

where x is a variable or a constant.

Example

In the following program, we will initialize a variable x with value of 10. We shall include this x in a string, and print the string to console.

main.swift

</>
Copy
var x = 10
var str = "Age is \(x)"
print(str)

Output

Swift - Include Value of a Variable in a String

Conclusion

In this Swift Tutorial, we learned how to include the value of a variable in a String in Swift programming, with the help of example program.