Printing without newline (print 'a',) prints a space, how to remove? [duplicate]
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?
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 a
s.
>>> 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>>>
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.
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.
You could print a backspace character ('\b'
):
for i in xrange(20):
print '\ba',
result:
aaaaaaaaaaaaaaaaaaaa
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='')
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.write('a')
sys.stdout.flush()
time.sleep(0.5)
sys.stdout.flush()
is necessary to force the character to be written each time the loop is run.
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.
printf("Hello,")
printf("World!")
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
Either what Ant says, or accumulate into a string, then print once:
s = '';
for i in xrange(20):
s += 'a'
print s
without what? do you mean
>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa
?
this is really simple
for python 3+ versions you only have to write the following codes
for i in range(20):
print('a',end='')
just convert the loop to the following codes, you don't have to worry about other things
WOW!!!
It's pretty long time ago
Now, In python 3.x it will be pretty easy
code:
for i in range(20):
print('a',end='') # here end variable will clarify what you want in
# end of the code
output:
aaaaaaaaaaaaaaaaaaaa
More about print() function
print(value1,value2,value3,sep='-',end='\n',file=sys.stdout,flush=False)
Here:
value1,value2,value3
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'
end='\n'
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'
file=sys.stdout
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
as simple as that
def printSleeping():
sleep = "I'm sleeping"
v = ""
for i in sleep:
v += i
system('cls')
print v
time.sleep(0.02)
in python 2, add (,) after print and it will act as end='' in python 3. But for space in between, you are still off to use future version of the print.
for x in ['a','b','c','d']:
print(x),
精彩评论