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





