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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In an ordinary Python string, n represents a line-feed character (LF). Put it between text, then pass the string to print() to display the text on separate lines. If you see the characters n in a shell or debugger instead, you may be looking at the string’s representation rather than its printed output.

Insert a line break with n

Type a backslash followed by n inside a single- or double-quoted string. Python interprets that pair as one LF character in the string value.

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

Output:

First line.
Second line.

The source code is on one physical line, but the value has a line-feed between the period and “Second.” When print() writes that value, the line-feed appears as a line break.

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

Why a shell may show n instead of a line break

A Python shell or debugger can display a representation of a string, which uses escape notation to show special characters unambiguously. For example, inspecting message may show 'First line.nSecond line.'. That representation does not mean the value contains a backslash and an n; it can be showing the notation for the LF character. Calling print(message) renders the line break and omits the enclosing quotes.

Keep the visible characters n

If the text should literally contain a backslash followed by n, prevent Python from interpreting that backslash as the start of an escape sequence. In an ordinary string literal, double the backslash; alternatively, prefix the literal with r to make it raw.

print('The escape is \n')
print(r'The escape is n')

Both calls print The escape is n. In the first literal, \ contributes one backslash to the value, followed by the ordinary letter n. In the second, the raw-string prefix keeps the backslash from being interpreted as an escape.

Use triple quotes for source text that spans lines

A triple-quoted string can include a physical newline in the source code itself. Choose this form when it is clearer to write the content across multiple source lines; use n when an explicit break inside a one-line literal is easier to read or needs to appear at a specific point.

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

This string also prints as two lines. The line break is part of the triple-quoted literal because the source text spans lines.

Distinguish n from r

n denotes ASCII Linefeed (LF), while r denotes the distinct ASCII Carriage Return (CR) character. They are not interchangeable escape spellings. Use n for the LF line break shown in the examples; the Python language reference defines the two escapes separately.

Avoid these newline mistakes

  • Wanting a line break but seeing n: Check whether you are viewing a representation in a shell or debugger, or whether the value actually contains a literal backslash and n. Use print() to render a string containing LF.
  • Wanting visible n text: Write \ in an ordinary literal or use a raw string such as r'n'.
  • Using a backslash at the physical end of a source line: That is not the same as inserting n into a string. In the relevant literal context, a backslash followed by a physical newline is an ignored end of line, not an LF character in the resulting value.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Reference

The Python Software Foundation’s Python 3.14.8 language reference, “Lexical analysis”, defines n as ASCII LF and explains escape processing, raw strings, doubled backslashes, and multiline literals. Its Python 3.10 tutorial, “An Informal Introduction to Python”, illustrates how print() renders escaped and special characters.

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.

Free tools Windows power users keep installed

One-click scans. No signup required.

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