liveBook · Manning

Newline

As explained in The Quick Python Book, Fourth Edition

A newline is a character or character sequence that marks the boundary between consecutive lines of text.

A newline marks the boundary between lines of text. In a Python string, `\n` represents a newline character: `"red\nblue"` contains two lines even though the string literal fits on one line. A normal quoted string cannot simply continue onto the next source-code line; use `\n` or a triple-quoted string. section 4.6

Display a newline

Evaluating `'a\n\tb'` interactively shows the escapes; `print('a\n\tb')` displays `a` and `b` on different lines, with a tab before `b`. `print()` also adds a final newline. When printing a file line that already has one, use `print(line, end="")` to avoid an extra line break. section 6.3.3

Newlines can be separators, data, or both:

  • `"\n".join(words)` places one newline between successive words. To write each word on its own line, including the last, write `word + "\n"` for each word or add a final `"\n"` after joining. section 6.10.2 section 10.8.2
  • `text.split("\n\n")` can separate paragraphs when a blank line is represented by two successive newlines. This depends on how the particular file marks paragraphs. section 21.2.2
  • `text.split("\n")` removes the separators. If you count characters in the resulting pieces, those newlines are missing: a four-line sample with three newlines was undercounted by three. Iterating over file lines and summing `len(line)` retains them in the count. section 8.13.2

Text-file line endings need not have the same representation. By default, Python translates recognized endings to `\n` when reading text. With `open(path, newline="")`, it recognizes the endings but returns them untranslated; `newline="\n"`, `"\r"`, or `"\r\n"` selects one ending to recognize. Binary reads return bytes without this text-mode translation. `write()` does not add a newline: include one in the string when needed. Text-mode writing may translate `\n` to the platform’s line ending. section 13.4

Keep an escape intact when generating Python code

If Python code containing `"\n"` is itself placed inside a triple-quoted string and written to a module file, the escape may become an actual line break too early, breaking the generated string literal. Make the outer string raw—`f.write(r'''...''')`—so the generated code retains `\n`. section 10.8.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains the `\n` escape, splitting and joining behavior, print output, and text-file translation; does not develop newline handling beyond these operations.
Examples
Shows printed escapes, word-per-line files, blank-line paragraph splitting, a character-count discrepancy, and a generated-module syntax error.
What it assumes
Assumes basic Python string and file operations while introducing `\n` as a string escape.
What you can do after
Apply it via Python’s `split()`, `join()`, `print()`, and `open()` to shape text and avoid newline-related output and counting errors.
How it connects
Connects newlines to escape sequences, string splitting, paragraph segmentation, file iteration, and text-mode file I/O.

Also explained in

Compare how 6 books explain newline →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.