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 without newline in Python?
In Python, the print() function adds a newline character by default at the end of each output. When you have multiple print statements, each output appears on a separate line. However, you can modify this behavior using the end parameter to print everything on a single line.
Normal Print() Behavior
By default, each print() statement ends with a newline character (\n), causing output to appear on separate lines ?
Example
print("Apple")
print("Mango")
print("Banana")
Output
Apple Mango Banana
Using the end Parameter
The end parameter controls what character(s) are printed at the end of each print() statement. By setting end="" or using a custom separator, you can print on the same line ?
Example with Comma Separator
print("Apple", end=",")
print("Mango", end=",")
print("Banana")
Output
Apple,Mango,Banana
Example with Space Separator
print("Apple", end=" ")
print("Mango", end=" ")
print("Banana")
Output
Apple Mango Banana
Example with No Separator
print("Apple", end="")
print("Mango", end="")
print("Banana")
Output
AppleMangoBanana
Alternative: Using sys.stdout.write()
Another approach is using sys.stdout.write(), which doesn't add a newline by default ?
import sys
sys.stdout.write("Apple")
sys.stdout.write("Mango")
sys.stdout.write("Banana")
sys.stdout.write("\n") # Add newline at the end
Output
AppleMangoBanana
Comparison
| Method | Syntax | Best For |
|---|---|---|
end="" |
print("text", end="") |
Simple cases, custom separators |
sys.stdout.write() |
sys.stdout.write("text") |
More control over output |
| Single print with commas | print("A", "B", "C") |
Static content |
Conclusion
Use the end parameter to control line endings in print statements. Set end="" for no separator, or use custom characters like commas or spaces to format your output on a single line.
