Explain Codes LogoExplain Codes Logo

How to print without a newline or space

python
printing
string-formatting
buffering
Alex KataevbyAlex Kataev·Aug 11, 2024
TLDR

To avoid newline or space in Python's print() function, use end='' like so:

print("Part1", end='') # Waiting for Part2... print("Part2") # Here it comes!

Output: Part1Part2, and yep, no newline or space.

Want your output flushed out straight away? Add a pinch of flush=True:

print("Processing...", end='', flush=True) # Outputs "Processing..." in real time, no buffering!

Advanced printing techniques

Newline? Not in Python 2!

In Python 2, stifling newlines might seem challenging but it's really not. Just add a sneaky comma after your print statement. Alternatively, bring in sys.stdout.write() for some extra muscle:

print "This won't have a newline", # or import sys sys.stdout.write("Neither will this one")

Buffer? What buffer?

Print and forget buffered outputs with this charming trio:

import sys sys.stdout.write("Processing step...") # Like print, but buffer hits different! sys.stdout.flush() # Boom! Buffer's gone.

No spaces? Easy peasy!

To marry multiple strings with no spaces in between, use sep='', like Romeo and Juliet without the space of that annoying wall:

print("hello", "world", sep='') # Output: "helloworld", because who needs spaces, right?

Printing nuances: Tricks up your print

The tale of the changing print

The story of Python's print function is one of constant evolution. From Python 3.3 onwards, you can control output buffering using flush. For those stuck in Python 2.7 or the early 3.x versions, you need to manually roll out sys.stdout.flush(). Yeah, we feel you!

from __future__ import print_function # A necessary evil for Python 2.x import sys print("Don't hold your breath...", end='') # No newline sys.stdout.flush() # ...because we've already flushed!

Concatenating strings: All joined at the hip

When concatenating strings without spaces, use the + operator, and for repeating sequences, 'string' * n will do the trick. It's really as simple as 1 + 1 = 11. Yup, you read it right!

print("This" + "That") # Output: "ThisThat", spaces are SO last season! print("echo" * 3, end='') # We all love a good echo, don't we?

Loops, formatting, and the works

Looping efficiently is the lifeblood of printing. Use looping with the magic of string formatting, and your printing jobs are sorted for life:

for character in "word": print(character, end='') # Newline? Ain't nobody got time for that!

Printf-style formatting with % is handy for string interpolation. Alternatively, str.format() or f-strings in Python 3.6+ give you more readable code:

print("The answer is %d. Mind. Blown." % 42, end='') # Well, that escalated quickly! print(f"The answer is {42}. Still mind blown.", end='') # F is for formatting!

Printing in loops: Continue the flow

When you use print() without any arguments, it adds a single newline—perfect for halting the loop party for a moment:

for _ in range(3): print('.', end='') # WAIT.FOR.IT... print() # ...and break! Phew, that was intense!