Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 210
pythonFoo Avatar asked Dec 21 '10 12:12

pythonFoo


People also ask

How do I get rid of space in print statement?

Use the sep Parameter of the print Statement in Python You can modify the spacing between the arguments of the print statement by using the sep parameter. The sep parameter can only be found and used in Python 3 and later versions. It can also be utilized for the formatting of the output strings.

How do I not print on newline?

Printing without a new line is simple in Python 3. In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")


2 Answers

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>>>  
like image 148
moinudin Avatar answered Oct 04 '22 10:10

moinudin


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.

like image 30
Antoine Pelisse Avatar answered Oct 04 '22 08:10

Antoine Pelisse