How to print without newline or space?


The question is in the title.

I'd like to do it in . What I'd like to do in this example in :

In C:

#include <stdio.h>

int main() {
    int i;
    for (i=0; i<10; i++) printf(".");
    return 0;



In Python:

>>> for i in xrange(0,10): print '.'
>>> for i in xrange(0,10): print '.',
. . . . . . . . . .

In Python print will add a \n or space, how can I avoid that? Now, it's just an example. Don't tell me I can first build a string then print it. I'd like to know how to "append" strings to stdout.

7/31/2019 8:39:38 AM

Accepted Answer

General way

import sys

You may also need to call


to ensure stdout is flushed immediately.

Python 2.6+

From Python 2.6 you can import the print function from Python 3:

from __future__ import print_function

This allows you to use the Python 3 solution below.

Python 3

In Python 3, the print statement has been changed into a function. In Python 3, you can instead do:

print('.', end='')

This also works in Python 2, provided that you've used from __future__ import print_function.

If you are having trouble with buffering, you can flush the output by adding flush=True keyword argument:

print('.', end='', flush=True)

However, note that the flush keyword is not available in the version of the print function imported from __future__ in Python 2; it only works in Python 3, more specifically 3.3 and later. In earlier versions you'll still need to flush manually with a call to sys.stdout.flush().


11/18/2018 4:07:45 AM

It should be as simple as described at this link by Guido Van Rossum:

Re: How does one print without a c/r ?

Is it possible to print something but not automatically have a carriage return appended to it ?

Yes, append a comma after the last argument to print. For instance, this loop prints the numbers 0..9 on a line separated by spaces. Note the parameterless "print" that adds the final newline:

>>> for i in range(10):
...     print i,
... else:
...     print
0 1 2 3 4 5 6 7 8 9

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow