Python print statement on new line
Categories:
Mastering Newlines in Python's print() Function

Explore how Python's print()
function handles newlines and discover techniques to control line breaks for cleaner output.
The print()
function in Python is a fundamental tool for displaying output to the console. By default, it automatically adds a newline character at the end of each statement, causing subsequent output to appear on a new line. While this behavior is often desirable, there are many scenarios where you might want to suppress this newline, add multiple newlines, or even replace the newline with a different separator. This article will guide you through the various ways to control newlines with print()
.
Understanding the Default Behavior
By default, the print()
function in Python 3 (and Python 2's print
statement) automatically appends a newline character (\n
) to the end of its output. This is controlled by the end
parameter, which defaults to \n
.
print("Hello")
print("World")
Default print()
behavior with newlines
The output of the above code will be:
Hello
World
Each print()
call starts on a new line because the previous one implicitly added \n
.
Suppressing the Newline Character
To prevent print()
from adding a newline, you can explicitly set the end
parameter to an empty string (''
) or any other desired character sequence. This is particularly useful when you want to concatenate output from multiple print()
statements on a single line.
print("This is on the same line", end='')
print(" as this.")
print("First part ", end='-')
print("Second part")
Using the end
parameter to control newlines
The output will be:
This is on the same line as this.
First part -Second part
As you can see, setting end=''
keeps the subsequent print()
output on the same line, while end='-'
replaces the newline with a hyphen.
end
parameter can be set to any string, allowing you to customize the termination of your printed output. Common alternatives include a space (' '
), a comma (','
), or a tab ('\t'
).Adding Multiple Newlines or Custom Separators
Beyond suppressing newlines, you can also use the end
parameter to add multiple newlines or to define a custom separator between items printed within a single print()
call. The sep
parameter controls what is printed between multiple arguments passed to print()
.
print("Line 1", end='\n\n') # Adds two newlines
print("Line 2")
print("Apple", "Banana", "Cherry", sep=' | ')
Using end
for multiple newlines and sep
for custom separators
The output will be:
Line 1
Line 2
Apple | Banana | Cherry
This demonstrates how end='\n\n'
creates an extra blank line, and sep=' | '
places a custom string between the arguments of the second print()
call.
flowchart TD A[Start print() call] B{Are multiple arguments provided?} C[Print first argument] D[Print 'sep' string] E[Print next argument] F{Are there more arguments?} G[Print 'end' string] H[End print() call] A --> B B -- Yes --> C C --> D D --> E E --> F F -- Yes --> D F -- No --> G B -- No --> C C --> G G --> H
Flowchart of Python's print()
function logic with sep
and end
Practical Applications
Controlling newlines is crucial for formatting console output, creating progress indicators, or generating specific file formats. For instance, when building a progress bar, you might want to update the same line repeatedly.
import time
for i in range(1, 11):
print(f"Processing item {i}/10...\r", end='')
time.sleep(0.5)
print("Done!")
Creating a simple progress indicator using \r
and end=''
In this example, \r
(carriage return) moves the cursor to the beginning of the current line without advancing to the next, allowing the print()
function to overwrite the previous output on the same line. Combined with end=''
, this creates a dynamic, single-line progress update.
\r
(carriage return) character is a powerful tool for dynamic console output, but its behavior can vary slightly across different terminal emulators. Always test your output in the target environment.