Featured post
python - Printing without newline (print 'a',) prints a space, how to remove? -
i have code:
>>> in xrange(20): ... print 'a', ... a a a a a a a a a a
i want output 'a'
, without ' '
this:
aaaaaaaaaaaaaaaaaaaa
is possible?
there number of ways of achieving result. if you're wanting solution case, use string multiplication @ant mentions. going work if each of print
statements prints same string. note works multiplication of length string (e.g. 'foo' * 20
works).
>>> print 'a' * 20 aaaaaaaaaaaaaaaaaaaa
if want in general, build string , print once. consume bit of memory string, make single call print
. note string concatenation using +=
linear in size of string you're concatenating fast.
>>> in xrange(20): ... s += 'a' ... >>> print s aaaaaaaaaaaaaaaaaaaa
or can more directly using sys.stdout.write(), print
wrapper around. write raw string give it, without formatting. note no newline printed @ end of 20 a
s.
>>> import sys >>> in xrange(20): ... sys.stdout.write('a') ... aaaaaaaaaaaaaaaaaaaa>>>
python 3 changes print
statement print() function, allows set end
parameter. can use in >=2.6 importing __future__
. i'd avoid in serious 2.x code though, little confusing have never used 3.x. however, should give taste of of goodness 3.x brings.
>>> __future__ import print_function >>> in xrange(20): ... print('a', end='') ... aaaaaaaaaaaaaaaaaaaa>>>
- Get link
- X
- Other Apps
Comments
Post a Comment