Printing without newline (print 'a',) prints a space, how to remove?

224,369

Solution 1

There are a number of ways of achieving your result. If you're just wanting a solution for your case, use string multiplication as @Ant mentions. This is only going to work if each of your print statements prints the same string. Note that it works for multiplication of any length string (e.g. 'foo' * 20 works).

>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa

If you want to do this in general, build up a string and then print it once. This will consume a bit of memory for the string, but only make a single call to print. Note that string concatenation using += is now linear in the size of the string you're concatenating so this will be fast.

>>> for i in xrange(20):
...     s += 'a'
... 
>>> print s
aaaaaaaaaaaaaaaaaaaa

Or you can do it more directly using sys.stdout.write(), which print is a wrapper around. This will write only the raw string you give it, without any formatting. Note that no newline is printed even at the end of the 20 as.

>>> import sys
>>> for i in xrange(20):
...     sys.stdout.write('a')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

Python 3 changes the print statement into a print() function, which allows you to set an end parameter. You can use it in >=2.6 by importing from __future__. I'd avoid this in any serious 2.x code though, as it will be a little confusing for those who have never used 3.x. However, it should give you a taste of some of the goodness 3.x brings.

>>> from __future__ import print_function
>>> for i in xrange(20):
...     print('a', end='')
... 
aaaaaaaaaaaaaaaaaaaa>>> 

Solution 2

From PEP 3105: print As a Function in the What’s New in Python 2.6 document:

>>> from __future__ import print_function
>>> print('a', end='')

Obviously that only works with python 3.0 or higher (or 2.6+ with a from __future__ import print_function at the beginning). The print statement was removed and became the print() function by default in Python 3.0.

Solution 3

You can suppress the space by printing an empty string to stdout between the print statements.

>>> import sys
>>> for i in range(20):
...   print 'a',
...   sys.stdout.write('')
... 
aaaaaaaaaaaaaaaaaaaa

However, a cleaner solution is to first build the entire string you'd like to print and then output it with a single print statement.

Solution 4

You could print a backspace character ('\b'):

for i in xrange(20):
    print '\ba',

result:

aaaaaaaaaaaaaaaaaaaa

Solution 5

Python 3.x:

for i in range(20):
    print('a', end='')

Python 2.6 or 2.7:

from __future__ import print_function
for i in xrange(20):
    print('a', end='')
Share:
224,369
pythonFoo
Author by

pythonFoo

Updated on January 21, 2020

Comments

  • pythonFoo
    pythonFoo over 4 years

    I have this code:

    >>> for i in xrange(20):
    ...     print 'a',
    ... 
    a a a a a a a a a a a a a a a a a a a a
    

    I want to output 'a', without ' ' like this:

    aaaaaaaaaaaaaaaaaaaa
    

    Is it possible?

  • Pär Wieslander
    Pär Wieslander over 13 years
    @Soulseekah: Yes, using only sys.stdout.write() is more convenient in this case. However, I wanted to show that writing an empty string to stdout suppresses the space between elements written using print, which could be useful in similar situations.
  • moinudin
    moinudin over 13 years
    @A A I read the question when the string multiplication answer was present, and thought I'd give an overview of several options. The others came while I was putting together my answer. It was accepted soon after, otherwise I would've likely deleted it.
  • Katriel
    Katriel over 13 years
    -1 Don't build up strings by concatenation; it's O(n^2) instead of O(n). Use "".join(...) instead.
  • user225312
    user225312 over 13 years
    I have no problem with your answer, it summarizes everything and that's good. But by doing this, you discourage other people who answer. There are many good answers and all deserve +1. Just imagine if everyone started making summaries.
  • user225312
    user225312 over 13 years
    @katrielalex: Does it matter? Has the OP asked for optimization or complexity? And will it matter for 20 strings? Come on, you seriously must be joking.
  • moinudin
    moinudin over 13 years
    @katrielalex Wrong, string concatenation using += in Python is a constant time operation for concatenation of a single character.
  • moinudin
    moinudin over 13 years
    I've started a discussion about this on meta
  • ctrl-alt-delor
    ctrl-alt-delor over 9 years
    Do you say that I can output an infinite amount of text in same time as outputting one character?
  • Tom
    Tom almost 8 years
    When I redefined printf to stdout.write, my stdout.write seems to be printing the number of chartacters the string is: "Hello,6" What gives? What's going on? And how do I stop it?
  • Tom
    Tom almost 8 years
    Also, can you explain what you mean by O(1) O(n)? I don't understand what that refers to
  • Benjamin R
    Benjamin R almost 8 years
    @Tom It's referring to complexity (Big O notation) as a reflection of the cost of the operation. O(1) is constant time - ideal - and O(n) is linear time - okay but way too expensive for something so simple. Although, for small things, the time you spend using your brain to worry about these things is more computationally wasteful than just using string concatenation.