This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 10 years ago.
I did not expect this, but:
print "AAAA",
print "BBBB"
Will output:
AAAA BBBB
With an extra space in the middle. This is actually documented.
How can I avoid that supurious space? The documentation says:
In some cases it may be functional to write an empty string to standard output for this reason.
But I do not know how to do that.
Three options:
Don't use two print statements, but concatenate the values:
print "AAAA" + "BBBB"
Use sys.stdout.write() to write your statements directly, not using the print statement
import sys
sys.stdout.write("AAAA")
sys.stdout.write("BBBB\n")
Use the forward-compatible new print() function:
from __future__ import print_function
print("AAAA", end='')
print("BBBB")
Get used to use print() function instead of the statement. It's more flexible.
from __future__ import print_function
print('foo', end='')
print('bar')
Related
for i in range(1, 4):
print i,
Output is 1 2 3, but I want the output like 123, i.e without spaces,
so how to do it?
As a technical note, while you should definitely not print this way if what you want is one string, it's doable with Python 3's print:
from __future__ import print_function
for i in range(1, 4):
print(i, end='')
See the other answers for how to do this building a string instead.
In python2, you have to use sys.stdout.write() to avoid the spaces inserted by the print command:
import sys
for i in range(1,4):
sys.stdout.write(str(i))
sys.stdout.flush()
Edit: as noted by GPhilo, you can also use python3's print function that has the option of avoiding separators, after importing it from __future__.
I am writing a program in Python and want to replace the last character printed in the terminal with another character.
Pseudo code is:
print "Ofen",
print "\b", # NOT NECCESARILY \b, BUT the wanted print statement that will erase the last character printed
print "r"
I'm using Windows8 OS, Python 2.7, and the regular interpreter.
All of the options I saw so far didn't work for me. (such as: \010, '\033[#D' (# is 1), '\r').
These options were suggested in other Stack Overflow questions or other resources and don't seem to work for me.
EDIT: also using sys.stdout.write doesn't change the affect. It just doesn't erase the last printed character. Instead, when using sys.stdout.write, my output is:
Ofenr # with a square before 'r'
My questions:
Why don't these options work?
How do I achieve the desired output?
Is this related to Windows OS or Python 2.7?
When I find how to do it, is it possible to erase manually (using the wanted eraser), delete the '\n' that is printed in python's print statement?
When using print in python a line feed (aka '\n') is added. You should use sys.stdout.write() instead.
import sys
sys.stdout.write("Ofen")
sys.stdout.write("\b")
sys.stdout.write("r")
sys.stdout.flush()
Output: Ofer
You can also import the print function from Python 3. The optional end argument can be any string that will be added. In your case it is just an empty string.
from __future__ import print_function # Only needed in Python 2.X
print("Ofen",end="")
print("\b",end="") # NOT NECCESARILY \b, BUT the wanted print statement that will erase the last character printed
print("r")
Output
Ofer
I think string stripping would help you. Save the input and just print the string upto the length of string -1 .
Instance
x = "Ofen"
print (x[:-1] + "r")
would give you the result
Ofer
Hope this helps. :)
This question already has answers here:
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 6 years ago.
I think the piece of code below is written in python 3 and my python 2 cannot run it. There is some problem with 'end'. How could I fix it? I don't know what is the logic behind end an i am very new to python
Any help much appreciated!
def myPrint(itp):
for i in range(10):
print("**",end=="")
for j in range(10):
print(itp[i][j],"**",end=="")
print()
You have two errors in your code, you need to replace both end=="" with end="". You are not supposed to compare the parameter end with an empty string, but you want to end the printing with an empty string, hence, do an assignment to the parameter end.
In Python 3.x, the end='' part will place a whatever parameter end is assigned with (here, an empty string) after the displayed string instead of a newline.
If you want to have the print functionality of python3 in python2, simply do an import :
from __future__ import print_function
(I am assuming the double == after end is a typo. end as a kwarg determines the end of line; the code likely originally was print("**",end="") with one = character)
To get print function semantics, you can set the __future__ flag print_function, by starting your file with
from __future__ import print_function
Be aware that the changes required to run a full Python 3 program under Python 2 are far more extensive than simply enabling some future flags (while you're add it, consider enabling unicode_literals). By far the easiest way to run a Python 3 program is to install a Python 3 interpreter.
The Python linebreak command \n doesn't work for me on Python 2.7 when I include something in the statement, like an int or a numpy array. Is there a way to do this? Here are some examples:
print("These \n linebreaks don't work:\n", 1)
"These \n linebreaks don't work:\n", 1
print("These \n work fine\n")
These
work fine
If you want to use print like a function, import the one from Python3.
>>> from __future__ import print_function
>>> print("These \n linebreaks don't work:\n", 1)
These
linebreaks don't work:
1
Now they actually do and you won't have to change anything.
Since you're using Python 2.7, you can't use parentheses using the default print keyword :
print "These \n linebreaks work fine :\n", 1
If you use parentheses, it will consider that you provide it a tuple, for example you were providing a tuple containing "These \n linebreaks don't work:\n" and 1.
EDIT : If you want to use the print function (like in Python 3), you should import it from the future like shown in timgeb's answer.
This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 7 years ago.
I want to do something like this.
calculating 50%
calculating 60%
calculating 70%
but in a single line.
Did hours of googling and couldn't find anything. :)
You can do something like this:
print '50%%',
print '\r60%%',
print '\r70%%',
The comma makes sure there is no newline. The \r clears the current line, so that the previous 50% is removed, and overwritten with the 60%. However, because the print is not flushed (like without the comma), it can happen that you will not see some lines printed. For that, you'll need to flush the output, by using this:
import sys
sys.stdout.flush()
It depends whether you want to overwrite the previous thing or to really write all three statements on one line.
The latter you can do by
import sys
sys.stdout.write("calculating 50%")
sys.stdout.write("calculating 60%")
sys.stdout.write("calculating 70%")
The former would be probably achieved by terminal escape sequences an would be different for different OSs.