Print without Trailing Newline
To print without a trailing newline in Swift, pass an empty string to the terminator
parameter of print()
function, along with the string you print.
The default value for terminator
is "\n"
. So, if we do not provide any value for terminator
during print()
function call, it prints a new line after printing all the items. To override this behaviour, and to not print a new line, do pass an empty string for terminator
.
Swift Example
In this example, we will print a greeting messages to command line output using print() function, but without a new line at the end.
main.swift
import Foundation
print("Hello World", terminator:"")
print("Hello User", terminator:"")
print("Hello World Again", terminator:"")
Program Output
Hello WorldHello UserHello World Again
Summary
We have learnt how to print without a new line at the end, in Swift.