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.
Related
This question already has answers here:
Using print() (the function version) in Python2.x
(3 answers)
regex (vim) for print ... to print(...) for python2 to python3
(3 answers)
vim regex for python2 print to python3
(1 answer)
Closed 4 years ago.
As the title says, how to replace all: print "string", in Python2 with: print("string"), for Python3?
I really wish they made this one function backward compatible. Officially the 2to3.py script is recommended, but it always seems like its just the print statements that i need to replace in Py2 script examples, say running in a notebook or Jupyter lab (which makes the 2to3.py less appealing - unless they implemented a converter!)
.
#Bazingaa, #Wiktor Stribiżew, #jpp: Not duplicate, i.e. not forwards compatiblity. I.E., I'm not asking how to use Python3 syntax in Python2, which would be forwards compatiblity, but the opposite which is reverse compatibility.
#jpp I'm shocked at your level of reading comprehension, even after adding clarification. I included those 'possible duplicates' originally in my answer below and explained the difference (which you must have also failed to read before adding your tags and comment), which do not 'give me what I'm looking for'. Appreciate the 'peril' caution, but again you missed the whole point, that I'm using Python 3.. trying to change the syntax from Python 2 in old tutorial scripts. Don't know how you could read that incorrectly twice. My solution below is valid, one I've looked for for years, and will undoubtedly help others who land here.
This and this show solutions for Vim, but this one works for Windows and print variable statements.
Works well in old Python2 example scripts. Otherwise use 2to3.py for additional conversions.
Try it out on regexr.com (doesn't work in NP++ for some reason):
find: (?<=print)( ')(.*)(')
replace: ('$2')
for variables:
(?<=print)( )(.*)(\n)
('$2')\n
for label and variable:
(?<=print)( ')(.*)(',)(.*)(\n)
('$2',$4)\n
They did make that function backwards compatible. Import it from future at the start of the file.
from __future__ import print_function
Well, forwards compatible. For now you're just with a find and replace in your ide.
I am new to python and I am having problem over the following syntax:
for x in range(0, 10):
print(x, ' ', end="")
I saw the syntax on a tutorial, however when I try it, it is giving me error. The goal I am trying to reach is printing 0 to 9 while eliminating new line. In other words, print 0 to 9 in a single line. Can you tell me what's wrong with the syntax if there is any?
To print in Python 2.7 without the line break you just need to add an extra comma to the end. It will also add a space between the numbers.
for x in range(0, 10):
print x,
Are you using python 2? Because print() with end keyword argument is a Python 3 command.
The print function with keyword end of Python 3 can be imported into Python 2 by
importing from __future__ at the very beginning of the script:
from __future__ import print_function
However, Python 2's print statement allows a syntax form without parentheses. This syntax will break with this import.
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:
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.
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')