How to print without a newline or space
How can you print output in Python without adding a newline or space? What techniques let you control exactly how text appears on the same line?
Printing output without a newline or extra space in Python is quite simple once you know the right tricks. By default, the print() function adds a newline at the end, so each call prints on a new line. But sometimes, you want to keep everything on the same line or control spacing precisely.
How to print without a newline:
Use the end parameter of the print() function:
print("Hello", end="")
print("World")
This will output:
HelloWorld
- By default, end="\n", which adds a newline. Changing it to an empty string "" prevents that.
- How to print without space between arguments:
- By default, print() separates multiple arguments with a space:
print("Hello", "World")
Outputs:
Hello World
To avoid the space, set the sep parameter to an empty string:
print("Hello", "World", sep="")
Outputs:
HelloWorld
Combining both for full control:
print("Hello", end="")
print("World", sep="")
You can also use this approach in loops to print on the same line without newlines or spaces:
for i in range(5):
print(i, end=" ")
This prints:
0 1 2 3 4
Summary:
- Use end="" to avoid newlines after print.
- Use sep="" to avoid spaces between multiple arguments.
- This lets you customize how your output appears, especially useful for progress bars, inline messages, or formatting.