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?
This question is related to
python
string
printing
python-2.x
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
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
without what? do you mean
>>> print 'a' * 20
aaaaaaaaaaaaaaaaaaaa
?
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)
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.
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
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='')
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
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.
Source: Stackoverflow.com