Swift Text Output

Use print() to write text and values to output.

Use string interpolation to combine values.


Print Text

Use print() to write text.

Interpolate values with \(expr).

Example

print("Hello, Swift!")
let name = "Kai"
print("Hello, \(name)!")

let a = 2, b = 3
print("Total: \(a + b)")

This example prints text and uses string interpolation to include values.



Print Without Newline

Use the terminator parameter to avoid a trailing newline.

Example

for n in 1...3 {
  print(n, terminator: " ") // prints on one line
}
print("done")

This example prints numbers on a single line by setting terminator to a space, then prints done on the next line.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *