Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

Python New Line: How to Use `n` in Strings (With Examples)

Use `n` inside an ordinary Python string to add an LF line break. See how `print()` renders it, when to use triple quotes, and how to preserve literal backslash-n text.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In an ordinary Python string, write n where you want an LF (line-feed) character. When you pass that string to print(), Python displays the line break. The backslash and n are escape notation in your source code; they are not two separate characters in the resulting string.

Insert a line break with n

Place the escape sequence inside a quoted string at the point where the next line should begin:

As an Amazon Associate I earn from qualifying purchases.

message = 'First line.nSecond line.'
print(message)

Output:

First line.
Second line.

The value stored in message has an LF character between the period and Second. print() renders that character as a line break. The same works with double quotes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
message = "First line.nSecond line."
print(message)

Python’s official tutorial explains that print() omits the enclosing quotes and prints escaped and special characters in readable form. The Python 3.14.8 language reference defines n as ASCII Linefeed (LF).

Why a REPL or debugger may show n

A string’s representation is designed to show its contents unambiguously, so it may display the escape spelling n instead of rendering a line break. That does not mean the string lacks a newline. Compare the representation with printed output:

message = 'First line.nSecond line.'
repr(message)
print(message)

The representation contains the visible escape spelling; the final call to print() displays the two lines. Use print() when you want to see how the text renders, and inspect the representation when you need to distinguish special characters from ordinary text.

Keep the literal backslash and n

If the string should contain the two visible characters backslash and n, prevent Python from interpreting them as a newline. In an ordinary string literal, double the backslash:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
print('The escape is \n')

Or use a raw string literal:

print(r'The escape is n')

Both print The escape is n on one line. In the first example, \ in the source literal contributes one backslash to the value, followed by the letter n. In the second, the r prefix means the backslash is not interpreted as an escape. Python’s language reference documents both ordinary and raw string escape behavior.

Use triple quotes for multiline source text

A triple-quoted string can span physical lines in your Python file. Choose this when it is clearer to write the content across source lines rather than place an explicit n inside a one-line literal:

message = '''First line.
Second line.'''
print(message)

This string contains a physical newline between the two lines of source text, and print() displays it as a line break. Use n when you want the break marked at a specific point inside a quoted line; use triple quotes when the multiline layout is easier to read. The language reference describes multiline string literals.

n is not r

Python defines n as ASCII Linefeed (LF) and r as ASCII Carriage Return (CR). They are distinct escape sequences, not interchangeable names for the same character. For a basic Python string line break, use n; do not assume CR and LF behave identically in every file format, protocol, or platform.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A backslash at the end of a source line is different

A backslash followed by a physical newline in a string literal is not the same as writing n. The language reference identifies backslash-newline as an ignored end of line in the relevant literal context. To put an LF character in the string, write the escape sequence n within the literal, or include a physical newline in a triple-quoted string.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.