Swift
Programming
Print
Newline
Code

print without newline in swift

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Swift’s print function appends a newline by default, which is usually helpful for console output. When you need output to continue on the same line, such as for progress displays or prompt-style interfaces, you have to override that behavior explicitly. The simplest answer is to use the terminator parameter.

Use print(..., terminator: "")

The built-in print function accepts a terminator argument. By default it is "\n", but you can replace it with an empty string.

swift
print("Loading", terminator: "")
print("...", terminator: "")
print("done")

This prints:

text
Loading...done

Only the last print call above adds a newline.

Add a Custom Separator and Terminator

print also accepts a separator parameter. That means you can control both how multiple values are joined and whether a newline is appended.

swift
let values = [1, 2, 3]
print(values[0], values[1], values[2], separator: " | ", terminator: "")
print(" <- end")

This is useful for formatted one-line status messages.

Build a Simple Progress Indicator

A common use case is showing progress dots without moving to a new line each time.

swift
1import Foundation
2
3for _ in 1...5 {
4    print(".", terminator: "")
5    fflush(stdout)
6    Thread.sleep(forTimeInterval: 0.2)
7}
8
9print(" done")

The fflush(stdout) call is important in command-line tools because buffered output may otherwise appear late.

Write Directly to Standard Output for More Control

If you need tighter control than print provides, write bytes directly.

swift
1import Foundation
2
3if let data = "Hello".data(using: .utf8) {
4    FileHandle.standardOutput.write(data)
5}
6
7if let data = " world".data(using: .utf8) {
8    FileHandle.standardOutput.write(data)
9}

This avoids print formatting entirely and is often useful in CLI tools or custom logging code.

Understand When Buffering Matters

For short scripts, print(..., terminator: "") is usually enough. In interactive terminal programs, however, output buffering can make it seem like nothing was printed yet.

That is why progress loops often combine:

  • 'terminator: ""'
  • 'fflush(stdout)'

Without flushing, the output may appear only after the program finishes or after enough buffered text accumulates.

Use a Reusable Helper in CLI Projects

If your command-line app prints many inline status messages, create a small helper.

swift
1import Foundation
2
3func inline(_ text: String) {
4    print(text, terminator: "")
5    fflush(stdout)
6}
7
8inline("Connecting")
9inline("...")
10print("ok")

This keeps the flushing logic in one place.

Avoid Confusing Logging with Inline Output

Standard logging usually benefits from line-based messages because they are easier to scan, store, and parse. Inline output is best for:

  • progress updates
  • prompts
  • animated terminal feedback

It is usually a poor fit for structured logs or long-running service diagnostics.

Playground and Xcode Differences

In Xcode Playgrounds or certain IDE consoles, buffering behavior can differ from a normal terminal. The code is still correct, but the environment may delay display updates.

If inline output seems inconsistent:

  • test in a real terminal
  • flush stdout
  • avoid assuming IDE console timing matches production CLI behavior

Common Pitfalls

One common mistake is calling print("text") and expecting it not to add a newline. It always does unless you override terminator.

Another issue is forgetting to flush output in a command-line progress loop, which makes inline updates appear late.

A third mistake is using inline printing for logs that should remain line-based and machine-readable.

Summary

  • Use print(..., terminator: "") to suppress the default newline in Swift.
  • Add fflush(stdout) when immediate terminal display matters.
  • Use separator and terminator together for one-line formatted output.
  • Write directly to FileHandle.standardOutput when you need lower-level control.
  • Keep inline printing for interactive CLI output, not general logging.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.