Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to print in same line in Python?
The print() function in Python automatically prints in the next line each time. By default, print() adds a newline character (\n) at the end of each output.
Default Behavior
Here's how print() works by default ?
for i in range(5):
print(i)
0 1 2 3 4
Using the end Parameter
The print() function accepts an end parameter that controls what gets printed at the end of each output. By changing this parameter, you can print on the same line.
Syntax
print("text", end="character")
Print on Same Line with Space
Use end=" " to print elements on the same line separated by spaces ?
for i in range(5):
print(i, end=" ")
0 1 2 3 4
Print on Same Line without Space
Use end="" to print elements directly next to each other ?
for i in range(5):
print(i, end="")
01234
Print with Custom Separators
You can use any character or string as the separator between elements ?
# Using comma separator
for i in range(5):
print(i, end=",")
print() # Add a newline after the loop
# Using dot separator
for i in range(5):
print(i, end=".")
0,1,2,3,4, 0.1.2.3.4.
Practical Examples
Here are some common use cases for same-line printing ?
# Creating a loading animation
import time
print("Loading", end="")
for i in range(3):
time.sleep(0.5)
print(".", end="")
print(" Done!")
# Printing a list on one line
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit, end=" | ")
Loading... Done! apple | banana | cherry |
Comparison Table
| Parameter | Result | Use Case |
|---|---|---|
end=" " |
Space between elements | Readable lists |
end="" |
No separation | Continuous strings |
end="," |
Comma-separated | CSV-like output |
end="\n" |
New line (default) | Normal printing |
Conclusion
Use the end parameter in print() to control line breaks. Set end=" " for space-separated output or end="" for continuous printing on the same line.
