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

PythonStringPrintingPython 2.x

Python Problem Overview

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:


Is it possible?

Python Solutions

Solution 1 - Python

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

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

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')

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='')

Solution 2 - Python

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

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('')

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

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

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



Solution 5 - Python

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='')

Solution 6 - Python

If you want them to show up one at a time, you can do this:

import time
import sys
for i in range(20):

sys.stdout.flush() is necessary to force the character to be written each time the loop is run.

Solution 7 - Python

Just as a side note:

Printing is O(1) but building a string and then printing is O(n), where n is the total number of characters in the string. So yes, while building the string is "cleaner", it's not the most efficient method of doing so.

The way I would do it is as follows:

from sys import stdout
printf = stdout.write

Now you have a "print function" that prints out any string you give it without returning the new line character each time.


The output will be: Hello, World!

However, if you want to print integers, floats, or other non-string values, you'll have to convert them to a string with the str() function.

printf(str(2) + " " + str(4))

The output will be: 2 4

Solution 8 - Python

without what? do you mean

>>> print 'a' * 20


Solution 9 - Python

Either what Ant says, or accumulate into a string, then print once:

s = '';
for i in xrange(20):
    s += 'a'
print s

Solution 10 - Python

this is really simple

for python 3+ versions you only have to write the following codes

for i in range(20):

just convert the loop to the following codes, you don't have to worry about other things

Solution 11 - Python


It's pretty long time ago

Now, In python 3.x it will be pretty easy


for i in range(20):
      print('a',end='') # here end variable will clarify what you want in 
                        # end of the code



More about print() function




you can print multiple values using commas

sep = '-'

3 values will be separated by '-' character

you can use any character instead of that even string like sep='@' or sep='good'


by default print function put '\n' charater at the end of output

but you can use any character or string by changing end variale value

like end='$' or end='.' or end='Hello'


this is a default value, system standard output

using this argument you can create a output file stream like

print("I am a Programmer", file=open("output.txt", "w"))

by this code you will create a file named output.txt where your output I am a Programmer will be stored

flush = False

It's a default value using flush=True you can forcibly flush the stream

Solution 12 - Python

as simple as that

def printSleeping():
     sleep = "I'm sleeping"
     v = ""
     for i in sleep:
         v += i
         print v


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpythonFooView Question on Stackoverflow
Solution 1 - PythonmoinudinView Answer on Stackoverflow
Solution 2 - PythonAntoine PelisseView Answer on Stackoverflow
Solution 3 - PythonPär WieslanderView Answer on Stackoverflow
Solution 4 - PythonLucas MoeskopsView Answer on Stackoverflow
Solution 5 - PythoncodeapeView Answer on Stackoverflow
Solution 6 - PythondaviewalesView Answer on Stackoverflow
Solution 7 - PythonKyle SiopioloszView Answer on Stackoverflow
Solution 8 - PythonAntView Answer on Stackoverflow
Solution 9 - PythonjensgramView Answer on Stackoverflow
Solution 10 - PythonShad MunirView Answer on Stackoverflow
Solution 11 - PythonSHAH MD IMRAN HOSSAINView Answer on Stackoverflow
Solution 12 - PythontlejmiView Answer on Stackoverflow