Printing Output
Prerequisites: Swift syntax basics
print is the everyday way to show output. It can take several
values, and two options change how they're joined and ended.
Example
print("Hello")
print("A", "B", "C")
print("A", "B", "C", separator: "-")
print("Loading...", terminator: "")
print(" done")
print(42)
print([1, 2, 3])
print()
print("End")
Expected output
Hello
A B C
A-B-C
Loading... done
42
[1, 2, 3]
End
Line by line:
- Several values are separated by a space by default.
separator:changes what goes between them.terminator:changes what goes at the end (a new line by default).terminator: ""keeps the next output on the same line.print()with nothing prints an empty line.
separator: and terminator: are argument labels — named
arguments. You'll see these throughout Swift, and the Functions
lessons explain how they work.
Practice
Print the numbers 1, 2, and 3 on one line separated by |.
Show solution
print(1, 2, 3, separator: " | ")
1 | 2 | 3