In Swift, outputting data to the console is essential for testing and debugging your code.
The most common way to display output is by using the print() function.
This function takes a value, converts it to a string format, and prints it to the standard output.
Let's look at the different ways we can output data in Swift.
print() FunctionYou can pass any literal value, string, or variable into the print() function.
By default, print() automatically adds a newline character at the end of the output.
print("Hello, Developer!")
print(42)
print(3.14159)
Because of the automatic newline, each of the examples above will print on a separate line in the console.
You can print the value stored inside a variable by simply passing the variable name into the print() function.
let currentYear = 2026 print(currentYear)
One of Swift's most powerful features for output is String Interpolation.
String interpolation allows you to construct a new string by inserting variables directly into a string literal.
You do this by wrapping the variable in parentheses and prefixing it with a backslash \().
let language = "Swift"
let version = 6.0
print("I am learning \\(language) version \\(version)!")
This is much cleaner than manually concatenating strings using the + operator!
If you want to print multiple things on the same line, you can change the default terminator of the print() function.
You can explicitly tell Swift what character to use at the end using terminator: "".
Which syntax is used for string interpolation in Swift?