development

How to Print a New Line in Python

Printing a new line in Python is commonly achieved with the print function’s default end parameter, which outputs a newline character ( \n ) after each call. You can also emit...

Mara Ellison
How to Print a New Line in Python

Overview and Core Answers

Printing a new line in Python is commonly achieved with the print function’s default end parameter, which outputs a newline character (\n) after each call. You can also emit newlines explicitly using \n within strings, the sep parameter for joining iterables, the file parameter for writing to files or streams, or platform‑specific line conventions when targeting different operating systems. This evergreen guide explains these mechanisms with examples that remain valid across stable Python versions, focusing on interpretable behavior and common pitfalls.

How print Writes New Lines by Default

The built‑in print function adds \n at the end of its output unless you change the end argument. This default makes each call start on a new line in interactive sessions and scripts. You can modify end to space items, suppress the newline, or write custom termination sequences. Understanding this behavior helps you control line breaks precisely and avoid extra blank lines.

Basic print New Line Examples

Calling print without arguments prints just the newline added by end, producing a blank line. You can pass multiple arguments; they are converted to strings, concatenated with sep (default ' '), and followed by end (default '\n'). These defaults are consistent across Python 3.x and make simple line output predictable.

Example patterns include:

  • print('A') outputs A then moves to the next line.
  • print('A', 'B', 'C') outputs A B C then moves to the next line.
  • print('A', end='') outputs A without advancing the cursor.
  • print('A', end=' | ') outputs A followed by a custom separator and continues on the same line.

New Line Characters in Strings

The newline escape sequence \n represents a line feed in Python strings. When printed, \n causes the cursor to move to the next line. On most platforms, writing \n to a text file opened in text mode translates appropriately to the platform’s native newline representation. Using \n inside strings gives you fine‑grained control over line breaks within a single print call.

Explicit Newline Use Cases

You can embed \n anywhere a string is accepted, such as in message headers, formatted reports, or log entries. Combining \n with triple‑quoted strings helps create multi‑line literals without multiple print calls. Be mindful that Windows writes \r\n natively, but Python’s universal newlines mode normalizes these differences when reading text files.

Controlling New Lines with sep and end

The sep parameter determines how multiple arguments are separated, while end sets what is appended after all arguments. Adjusting these parameters lets you build compact or verbose output formats without manual string concatenation. Choosing clear separators and termination rules improves readability of logs and command‑line tools.

Consider the following patterns:

ExpressionResult (followed by cursor position)Use Case
print('x', 'y', 'z')x y z Default spaced output with line break
print('x', 'y', 'z', sep=',')x,y,z Comma‑separated values
print('x', 'y', 'z', sep='|', end='!!\n')x|y|z!! Custom separator and termination
print('a', 'b', end='')abSuppress newline, continue on same line

Writing New Lines to Files and Streams

The file parameter redirects print output to any object that supports the write method, such as an open file or sys.stdout. When writing text, newline handling depends on the platform and the file’s newline setting. Opening files in text mode with newline='' enables universal newlines mode, which normalizes line endings when reading and writing across Windows, macOS, and Linux.

File and Stream Tips

  • Use print('line', file=open('out.txt', 'w', newline='')) for cross‑platform text output.
  • Binary files require explicit encoding; for example, encode strings to bytes before writing.
  • Flushing output with flush=True is helpful for long‑running scripts where real‑time visibility matters.

Platform and Newline Nuances

Operating systems represent newlines differently: Unix and macOS use \n (line feed), Windows uses \r\n (carriage return + line feed), and legacy macOS used \r. Python’s text mode normalizes these differences, so reading \r\n from a Windows file yields \n internally. Writing \n in text mode translates to the platform’s native newline sequence when the file is flushed or closed, unless newline='' is specified.

Best Practices and Cautions

Prefer explicit control over end and sep when formatting output, and avoid relying on side effects of print in production logging. For precise newline handling in files, open with newline='' in text mode or manage binary encoding manually. In REPL and notebooks, remember that the last expression’s value is displayed separately from print, which can affect perceived output spacing.

tags: python, print, newline, output formatting

Related Reading

More pages in this topic cluster.

For i in range 4: A Practical Guide to Python’s Range-Based Loop

In Python, the expression for i in range(4): iterates four times, with i taking the values 0, 1, 2, and 3. This sequence starts at 0 by default and stops before the stop value,...

Read next
Mermaid Recipe: A Technical Guide to Diagram-as-Code Syntax and Usage

Mermaid is a diagramming and charting tool that uses text-based definitions to generate flowcharts, sequence diagrams, class diagrams, Gantt charts, and more directly in the bro...

Read next
How to View a Website's Code

To view a website's code is to inspect the technologies, rules, and structure that define its layout, behavior, and content in a web browser. Most modern browsers ship with deve...

Read next