The Python Oracle

How to print without a newline or space

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Isolated

--

Chapters
00:00 How To Print Without A Newline Or Space
00:29 Accepted Answer Score 3295
02:03 Answer 2 Score 302
02:44 Answer 3 Score 179
03:15 Answer 4 Score 100
03:58 Answer 5 Score 48
04:10 Thank you

--

Full question
https://stackoverflow.com/questions/4933...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 3336


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
sys.stdout.write('.')

You may also need to call

sys.stdout.flush()

to ensure stdout is flushed immediately.




ANSWER 2

Score 177


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
..........



ANSWER 3

Score 99


Use the Python 3-style print function for Python 2.6+ (it will also break any existing keyworded print statements in the same file).

# For Python 2 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 Python 2 print keywords, create a separate printf.py file:

# printf.py

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):
    printf('.')
print 'done'
#..........done

More examples showing the printf style:

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



ANSWER 4

Score 47


How to print on the same line:

import sys
for i in xrange(0,10):
   sys.stdout.write(".")
   sys.stdout.flush()