Skip to main content

How to Put a Line Break in a Python String with \n (Newline)

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

You can't press Enter in the middle of an ordinary Python string, because the string has to end on the line where it started. To put a line break inside it, you type \n, a backslash and the letter n, and Python turns that into a new line. This guide does it on Python 3.15.2.

Syntax​

Syntax
"<first line>\n<second line>"
PartWhat it means
<first line>The text before the line break
\nThe newline escape sequence: a backslash followed by n. Python stores it as one line-break character
<second line>The text that starts on the next line

The language reference explains that "Inside a string literal, the backslash (\) character introduces an escape sequence, which has special meaning depending on the character after the backslash." Its table lists \n as "ASCII Linefeed (LF)", the character that starts a new line.

The Example​

The script prints three things: one string that comes out on two lines, a string with an empty line between two words, and the length of "\n" on its own.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

lines.py
print("First line\nSecond line")
print("Above\n\nBelow")
print(len("\n"))

The lines.py script in a macOS-style code window: it prints First line and Second line separated by a newline escape, Above and Below separated by two of them, and the length of a newline on its own.

  • "First line\nSecond line" is one string. The \n in the middle marks where the line breaks.
  • "Above\n\nBelow" has two line breaks in a row, so the second one starts a line with nothing on it.
  • len("\n") counts the characters in a string that holds only the escape sequence.

Step 2: Run It​

Terminal
uv run python lines.py
Output
First line
Second line
Above

Below
1

The terminal after uv run python lines.py: First line and Second line on separate lines, Above, an empty line and Below, then 1.

The first string came out on two lines, and the second left an empty line between Above and Below. The 1 is the point to notice: you typed two characters, a backslash and an n, but the string holds one, the line break itself. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Typing a forward slash: "First line/nSecond line"No line break. Python prints the text as typed: First line/nSecond lineUse a backslash: "First line\nSecond line"

FAQ​

How do I keep \n as two characters?​

Put an r in front of the string to make it a raw string. print(r"First line\nSecond line") printed First line\nSecond line, on one line. The language reference says escape sequences are interpreted "Unless an ‘r’ or ‘R’ prefix is present", so in a raw string a backslash stays a backslash.

Which other escape sequences are there?​

The escape sequences table in the language reference lists them all, such as \t for a tab, \\ for a backslash and \" for a double quote.

Conclusion​

\n inside a string started a new line, \n\n left an empty line, and len("\n") showed that the two characters you type become a single line-break character.

Additional Resources​