New Line in Python: How to Write, Print and Verify Line Breaks
Learn how to add a new line in Python with \n, print(), triple-quoted strings and join(), how raw strings and escaping change it, and how to check the result with repr().
To start a new line in a Python string, write the escape sequence \n inside the quotes. This guide covers that method and the other common ways to produce line breaks: print(), triple-quoted strings, joining lists, f-strings and files. It also shows how to check that the string contains what you intended. The code samples are illustrative examples. The outputs shown follow Python's documented behavior, but run them on your own Python version to confirm.
What a newline does in a Python string
A newline is an invisible control character called line feed (LF, Unicode code point U+000A). When text containing it is displayed or saved, everything after it starts on the next line. You cannot see it in source code, so Python lets you write it as an escape sequence. An escape sequence is a backslash followed by a letter, and Python reads the pair as a single special character. In a normal string literal, \n is stored as one character, not as a backslash and the letter n.
text = "first line\nsecond line"
print(text)
print(len("\n"))
Expected output:
first line
second line
1
Step 1: Put \n inside a string literal
Put \n exactly where the break should go. It works the same way in single-quoted and double-quoted strings, and you can repeat it to create blank lines. For example, "a\n\nb" prints a, then an empty line, then b.
greeting = 'Hello\nWorld'
spaced = "a\n\nb"
print(greeting)
print(spaced)
Step 2: Control the line break that print() adds
print() already ends its output with a newline because its end parameter defaults to "\n". You can change this:
- Pass end="" to keep the next output on the same line.
- Call print() with no arguments to output just a line break.
- Use sep="\n" to put each argument on its own line.
print("Hello")
print("World", end="")
print()
print("a", "b", "c", sep="\n")
Expected output:
Hello
World
a
b
c
Step 3: Use triple quotes for multi-line text
Text wrapped in triple quotes (""" or ''') keeps every real line break you type, so you do not need to write \n. Two details matter:
- A line break right after the opening quotes is included. Put a backslash at the end of that first line to suppress it.
- Indentation inside the quotes becomes part of the string.
message = """Dear reader,
This is line two.
"""
print(repr(message))
compact = """\
Line one
Line two"""
print(repr(compact))
Expected output:
'Dear reader,\nThis is line two.\n'
'Line one\nLine two'
Step 4: Join a list of lines
When the lines are already in a list, "\n".join(lines) places a newline between items. It does not add one after the last item, so append "\n" yourself if the text must end with a line break, as many text files do.
lines = ["alpha", "beta", "gamma"]
text = "\n".join(lines)
print(repr(text))
text_with_final_break = "\n".join(lines) + "\n"
Expected output of the print call:
'alpha\nbeta\ngamma'
Step 5: Check the result with repr()
Newlines are invisible in normal output, so print(repr(s)) is the most reliable check. repr() shows the string as a literal, with every newline written as \n. You can also count line breaks with s.count("\n") or test for one with "\n" in s.
s = "one\ntwo"
print(repr(s))
print(s.count("\n"))
print("\n" in s)
Expected output:
'one\ntwo'
1
True
When \n must stay as two literal characters
Sometimes you need the backslash and the n themselves, for example in a Windows path, a regular expression or text that will be pasted into another program's source code. You have two options:
- Use a raw string by adding the prefix r, as in r"\n".
- Double the backslash, as in "\n".
- Both produce a two-character string.
print(len(r"\n"))
print(len("\\n"))
print(r"C:\new\folder")
Expected output:
2
2
C:\new\folder
To turn real line breaks into visible \n text for one-line storage, and then restore them, replace() is the simplest approach. It has one limitation: if the original text already contained a literal backslash followed by n, the reverse replacement cannot tell the two apart. In that case, use json.dumps() and json.loads(). They escape and unescape newlines and backslashes consistently.
original = "line 1\nline 2"
escaped = original.replace("\n", "\\n")
print(escaped)
restored = escaped.replace("\\n", "\n")
print(restored == original)
import json
encoded = json.dumps(original)
print(encoded)
print(json.loads(encoded) == original)
Expected output:
line 1\nline 2
True
"line 1\nline 2"
True
Newlines in f-strings
In an f-string, \n works normally in the literal text part. Backslashes inside the curly-brace expressions were not allowed before Python 3.12. On older versions, store the newline in a variable and use that inside the braces. This works on any version.
name = "Ada"
print(f"Hello,\n{name}")
nl = "\n"
items = ["x", "y"]
print(f"Items:{nl}{nl.join(items)}")
Newlines when reading and writing files
Files opened in text mode translate line endings by default. When writing, each \n becomes the platform's line separator, available as os.linesep; on Windows that is \r\n. When reading, \r\n and \r are converted to \n, so your code sees only \n. The newline argument of open() changes this:
- newline="" turns off translation. The csv module documentation recommends it when opening CSV files.
- newline="\n" writes plain \n on every platform.
| Escape | Name | Typical use |
| \n | Line feed (LF) | Linux, macOS and Python's internal line ending |
| \r\n | Carriage return + line feed (CRLF) | Windows text files and many network protocols |
| \r | Carriage return (CR) | Older classic Mac OS files |
with open("notes.txt", "w", encoding="utf-8") as f:
f.write("first\n")
f.write("second\n")
with open("notes.txt", encoding="utf-8") as f:
for line in f:
print(repr(line))
Expected output:
'first\n'
'second\n'
Each line read from a file keeps its trailing \n. Use line.rstrip("\n") to remove only the newline. A bare strip() also removes spaces and tabs, which may be meaningful content. To split a whole text into lines without the breaks, splitlines() is usually cleaner than split("\n"). For example, "a\nb\n".split("\n") returns ['a', 'b', ''], while splitlines() returns ['a', 'b']. Note that splitlines() also splits on other line boundaries, including \r, \v, \f, \x85, \u2028 and \u2029. If only \n should count, use split("\n").
Common mistakes
- Writing /n with a forward slash: it is ordinary text, not a newline.
- Expecting writelines() to add line breaks: it writes the items as they are, so each item must already end with \n.
- Confusing a backslash at the end of a code line with a newline: outside a string, it only continues the statement on the next line.
- Putting \n in a raw string and expecting a line break: r"\n" stays as two characters.
- Getting double line breaks when printing lines read from a file: each line already ends with \n and print() adds another. Use print(line, end="") or strip the newline first.
Quick reference
| Goal | Code |
| Break a line inside a string | "first\nsecond" |
| Print without the automatic newline | print(x, end="") |
| Print each argument on its own line | print(a, b, sep="\n") |
| Multi-line text block | """line one |
| line two""" |
| Join list items into lines | "\n".join(items) |
| Show hidden newlines | print(repr(s)) |
| Keep backslash-n as text | r"\n" or "\n" |
| Remove the trailing newline | line.rstrip("\n") |
| Split text into lines | text.splitlines() |
| Write plain \n endings on any OS | open(path, "w", newline="\n") |