How to Print Multiple Lines in Python
To print multiple lines in Python, put \n between the lines in a string and pass it to print(). You can also use separate print() calls, triple-quoted strings, or a loop, depending on how your text is stored. The examples below show each method and how to avoid extra blank lines or unwanted indentation.
How Python Prints Newlines
In a normal Python string, \n represents a newline character. When you print that string, the text after the newline starts on the next output line.
The print() function also controls spacing in two ways:
sepsets the separator between arguments. Its default is a space.endsets what follows the final argument. Its default is a newline.
That means separate print() calls normally produce separate output lines, but passing several arguments to one call normally puts them on the same line.
How to Print Text on Separate Lines
Choose a method based on the text you already have:
| What You Want to Print | Method to Use |
|---|---|
| A short message with explicit line breaks | Put \n inside the string |
| Messages produced at different points in your code | Use separate print() calls |
| Several values, each on its own line | Use print() with sep="\n" |
| A fixed block of multiline text | Use triple quotes |
| A message containing variable values | Use an f-string with \n |
| Items from a list or another iterable | Use a loop, or join strings with "\n".join() |
Step 1: Write a String With Newline Characters
- Put your text inside ordinary single or double quotes.
- Insert
\nwherever the next output line should begin, then pass the string toprint().
print("First line\nSecond line\nThird line")
Use a backslash followed by n, not a forward slash.
Step 2: Run the Code and Check the Output
- Run the example in a Python interpreter or as part of a Python script.
- Confirm that the output contains separate lines rather than visible
\ncharacters.
Output:
First line
Second line
Third line
You do not need to add a newline after the final word: print() supplies its usual line ending.
Other Ways to Print Multiple Lines
Separate calls, separators, triple-quoted strings, f-strings, and iterables can produce multiple output lines for different kinds of text.
Separate Print Calls
Use individual calls when each message belongs to a different statement or stage of your program.
print("Connecting...")
print("Connected.")
Output:
Connecting...
Connected.
This is often the clearest choice for status messages. There is no need to combine unrelated messages into one string just to reduce the number of calls.
Multiple Arguments With sep
When the values are already separate, set sep="\n" to place each argument on its own line.
print("Name: Alice", "Status: ready", sep="\n")
Output:
Name: Alice
Status: ready
The separator goes between arguments. It does not split a single string into lines.
For example, passing one string containing spaces will not make sep replace those spaces. You must supply separate arguments or include newline characters in the string.
Triple-Quoted Strings
Triple quotes let you write literal line breaks directly into a string. Both triple single quotes and triple double quotes work.
print("""Welcome to the program.
Choose an option:
Start
Exit""")
Output:
Welcome to the program.
Choose an option:
Start
Exit
This format works well for menus, help text, and other fixed blocks.
Triple-quoted strings preserve their whitespace. Putting the opening or closing quotes on a line of their own introduces a boundary newline, and indenting the text introduces spaces into the output.
F-Strings With Variables
Use an f-string when each line includes a variable or expression.
name = "Alice"
age = 30
print(f"{name} is {age} years old.\nWelcome to the program.")
Output:
Alice is 30 years old.
Welcome to the program.
For a longer template, combine the f prefix with triple quotes:
name = "Alice"
status = "Ready"
print(f"""User: {name}
Status: {status}
You can begin.""")
Output:
User: Alice
Status: Ready
You can begin.
Choose the form that makes the template easiest to read; both support the same variable substitution.
Lists and Other Iterables
A loop prints each item as it becomes available:
lines = ["First line", "Second line", "Third line"]
for line in lines:
print(line)
Output:
First line
Second line
Third line
This works with lists, generators, and other iterables. It also handles non-string items because print() converts its arguments to text.
If you need a reusable string containing the entire block, join the items first:
lines = ["First line", "Second line", "Third line"]
message = "\n".join(lines)
print(message)
Output:
First line
Second line
Third line
Every item passed to str.join() must be a string. For mixed types, a printing loop is often simpler than converting the items just to join them.
For a short list, unpacking is another compact option:
lines = ["First line", "Second line", "Third line"]
print(*lines, sep="\n")
The * passes each item as a separate argument, producing the same visible output.
One useful edge case: a loop over an empty list prints nothing, while printing an empty joined string still writes a newline. Unpacking an empty list into print() also writes a newline. Use the loop when an empty collection should produce no output.
How to Wrap Long Strings in Python Code
Wrapping Python code does not automatically wrap its printed output.
When wrapping Python source, keep each code line within PEP 8’s recommended maximum of 79 characters. Put adjacent string literals inside parentheses to spread a long expression across source lines without adding output line breaks.
message = (
"This message is written across several source lines, "
"but it prints as one continuous line."
)
print(message)
Output:
This message is written across several source lines, but it prints as one continuous line.
Python concatenates those adjacent literals. It does not insert spaces between them, so the first literal deliberately ends with a space.
If you want both readable source formatting and separate output lines, include newline characters explicitly:
message = (
"Download complete.\n"
"Checking the files.\n"
"Ready to install."
)
print(message)
Output:
Download complete.
Checking the files.
Ready to install.
Parentheses avoid the need for source-line continuation backslashes. A physical line break inside an ordinary single- or double-quoted string, without valid continuation syntax, causes a syntax error.
How to Check and Fix Multiline Output
Inspect string contents, trailing newlines, indentation, and terminal wrapping to identify why multiline output differs from the intended layout.
Literal Backslash-N Characters
If the console displays \n instead of starting a new line, inspect how the string was created.
Two common causes are:
- A raw string, such as
r"First line\nSecond line". - An escaped backslash, such as
"First line\\nSecond line".
Both contain a literal backslash followed by n. For an intended line break, use an ordinary string containing \n.
To inspect a string’s contents, print its representation:
message = "First line\nSecond line"
print(repr(message))
Output:
'First line\nSecond line'
Seeing \n in repr() output is normal: the representation makes actual newline characters visible. A literal backslash followed by n appears as \\n in that representation.
Extra Blank Lines
An extra blank line often means the string already ends with a newline and print() adds another.
When constructing your own message, leave the trailing newline out of the string:
message = "Task finished."
print(message)
Output:
Task finished.
Internal consecutive newline characters also create blank lines. Keep them only when that spacing is intentional.
Unwanted Indentation
For a triple-quoted block indented in your source, use textwrap.dedent() to remove common leading whitespace:
from textwrap import dedent
message = dedent("""\
First line
Second line""")
print(message)
Output:
First line
Second line
Here, dedent() removes the shared indentation. The backslash immediately after the opening triple quotes suppresses the initial newline, while the closing quotes sit after the final text to avoid adding a trailing newline to the string.
Unlike stripping every line individually, dedent() preserves indentation relative to the rest of the block.
Visual Wrapping Versus Actual Newlines
A narrow terminal can wrap a long line even when the string contains no newline characters. Widening the terminal changes that visual wrapping, but it does not change explicit line breaks.
Before keeping your implementation, check that:
- Each intended output line begins in the right place.
- Blank lines and leading spaces are intentional.
- Empty input behaves as expected.
- Terminal wrapping is not being mistaken for a newline in the string.
Run the chosen method with your actual text or collection, then keep the simplest version that produces the intended layout.
Frequently Asked Questions
What Is the Difference Between splitlines() and split("\n") in Python?
splitlines() splits a string at recognized line boundaries, while split("\n") splits only at newline characters. splitlines() handles boundaries such as \r\n without leaving a carriage return attached to the text, and it does not create an extra empty item for a final line break. Neither method prints anything; both return a list of strings.
text = "First line\nSecond line\n"
print(text.splitlines())
print(text.split("\n"))
Output:
['First line', 'Second line']
['First line', 'Second line', '']
How Can I Print Multiple Lines of Python Code Without Running Them?
Store the code in a string and print that string; printing source text does not execute it. A raw triple-quoted string is useful when the code you want to display contains escape sequences that must remain visible.
code = r'''print("First line\nSecond line")
print("Done")'''
print(code)
Output:
print("First line\nSecond line")
print("Done")
How Do I Print Lines From a File Without Adding Blank Lines?
Use a loop with print(line, end="") so Python does not append another newline to each line read from the file. Text-file iteration normally retains each line’s existing newline, except when the final line has no line ending. Replace notes.txt with the path to your UTF-8 text file.
with open("notes.txt", encoding="utf-8") as file:
for line in file:
print(line, end="")
Can one print() call produce two or more output lines in Python?
Yes. Include \n in a regular string wherever the following output line should start, then pass that string to print(). This lets a single print() call create a two-line or longer result.
