0
0
PythonHow-ToBeginner · 3 min read

How to Print on Same Line in Python: Simple Guide

In Python, to print on the same line, use the print() function with the end='' parameter to avoid moving to a new line. For example, print('Hello', end='') prints without adding a newline, so the next print continues on the same line.
📐

Syntax

The print() function normally ends with a newline, moving the cursor to the next line. To print on the same line, use the end parameter to change what is printed at the end. By default, end='\n' (newline). Setting end='' prints nothing after the output, so the next print stays on the same line.

  • print(value, end=''): prints value without moving to a new line.
  • You can set end to any string, like a space ' ' to separate outputs on the same line.
python
print('Hello', end='')
print(' World!')
Output
Hello World!
💻

Example

This example shows printing multiple values on the same line by setting end=' ' to add a space instead of a newline. It prints numbers 1 to 5 on one line separated by spaces.

python
for i in range(1, 6):
    print(i, end=' ')
print('Done')
Output
1 2 3 4 5 Done
⚠️

Common Pitfalls

A common mistake is forgetting to set end='' or end=' ', so each print() outputs on a new line by default. Another is mixing print() with input() without flushing output, which can cause unexpected line breaks.

Also, using commas in print() adds spaces but still ends with a newline unless end is set.

python
print('Hello')
print('World')  # Prints on two lines

# Correct way:
print('Hello', end=' ')
print('World')  # Prints on same line
Output
Hello World Hello World
📊

Quick Reference

Use this quick guide to remember how to print on the same line:

UsageDescriptionExample
Default printPrints with newlineprint('Hi')
Print same lineSet end to empty stringprint('Hi', end='')
Print with spaceSet end to spaceprint('Hi', end=' ')
Print multiple valuesSeparate by commas, ends with newlineprint('Hi', 'there')

Key Takeaways

Use print(value, end='') to print without moving to a new line.
Set end=' ' to add a space instead of a newline between prints.
By default, print() ends with a newline, so output moves to the next line.
For loops can print on the same line by setting end parameter.
Remember to flush output if mixing print with input for smooth display.