How to print without newline or space?


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 range(10): print('.')
>>> 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.

1/1/2020 9:34:01 PM

Accepted Answer

In Python 3, you can use the sep= and end= parameters of the print function:

To not add a newline to the end of the string:

print('.', end='')

To not add a space between all the function arguments you want to print:

print('a', 'b', 'c', sep='')

You can pass any string to either parameter, and you can use both parameters at the same time.

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

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

Python 2.6 and 2.7

From Python 2.6 you can either import the print function from Python 3 using the __future__ module:

from __future__ import print_function

which allows you to use the Python 3 solution above.

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(). You'll also have to rewrite all other print statements in the file where you do this import.

Or you can use sys.stdout.write()

import sys

You may also need to call


to ensure stdout is flushed immediately.

1/1/2020 9:42:50 PM

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

Note: The title of this question used to be something like "How to printf in python?"

Since people may come here looking for it based on the title, Python also supports printf-style substitution:

>>> strings = [ "one", "two", "three" ]
>>> for i in xrange(3):
...     print "Item %d: %s" % (i, strings[i])
Item 0: one
Item 1: two
Item 2: three

And, you can handily multiply string values:

>>> print "." * 10

Use the python3-style print function for python2.6+ (will also break any existing keyworded print statements in the same file.)

# for python2 to use the print() function, removing the print keyword
from __future__ import print_function
for x in xrange(10):
    print('.', end='')

To not ruin all your python2 print keywords, create a separate file


from __future__ import print_function

def printf(str, *args):
    print(str % args, end='')

Then, use it in your file

from printf import printf
for x in xrange(10):
print 'done'

More examples showing printf style

printf('hello %s', 'world')
printf('%i %f', 10, 3.14)
#hello world10 3.140000

How to print on the same line:

import sys
for i in xrange(0,10):

The new (as of Python 3.x) print function has an optional end parameter that lets you modify the ending character:

print("HELLO", end="")



There's also sep for separator:

print("HELLO", "HELLO", "HELLO", sep="")



If you wanted to use this in Python 2.x just add this at the start of your file:

from __future__ import print_function


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]