Preloader
Swift
  • Estimated reading time: 1 Minute

How to print content to the console without a new line in Swift with the print function

To describe graphically the issue that a lot of developer face with this simple instruction on Swift, we'll analize the following code and you will need to guess the output:

print(1)
print(2)
print(3)

As a developer that doesn't have any contact at all with the swift language may simply think that the print function is an equivalent of a echo (from PHP) or printf (from C) function, so the guessed output should be:

123

However, Swift doesn't respect the rules because he's a badass and does what he wants and will print instead:

1
2
3

In this article, we'll explain briefly why this happens and how to prevent this behaviour in your code.

Printing content on the same line

Since Swift 3, the documentation of the print method is the following one:

public func print<Target>(_ items: Any..., separator: String = default, terminator: String = default, to output: inout Target) where Target : TextOutputStream

By default the print function writes the textual representations of the given items into the standard output on a single line, everytime the function is used. This behaviour can be easily overriden defining the terminator parameter as an empty string:

Terminator Parameter Swift

Not that terminator, but specifically the terminator: String parameter of the print method. For example, to fix the expected output of the initial example, we could just change the parameter to an empty string:

print(1, terminator:"")
print(2, terminator:"")
print(3, terminator:"")

And the output of our code will be (as expected by any developer):

123

Happy coding !

Share:
Carlos Delgado

Carlos Delgado

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Related articles
How to add a CocoaPods dependency in your CapacitorJS Plugin for iOS
23 Aug, 2021
  • Estimated reading time: 2 Minutes
How to implement a file and directory picker in MacOS using Swift 5
5 Jan, 2020
  • Estimated reading time: 4 Minutes
How to retrieve the serial number of a mac with Swift
4 Jan, 2020
  • Estimated reading time: 1 Minute
How to print the Pascal's triangle in Swift
19 Mar, 2019
  • Estimated reading time: 2 Minutes
How to work with the Swift Programming Language in Windows
10 Mar, 2019
  • Estimated reading time: 5 Minutes
Weekly trending
The Hidden Costs of NOT Having Managed IT Services
19 Jun, 2026
  • Estimated reading time: 10 Minutes
How Professional Electricians Help Protect Your Home and Family
19 Jun, 2026
  • Estimated reading time: 5 Minutes
How AI Identifies Antiques From Photos
19 Jun, 2026
  • Estimated reading time: 14 Minutes
Our Sponsors

Our blog is proudly supported by industry-leading sponsors.