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.
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. :)
i = 1
while i <=5:
print (i)
i=i + 1
print ("ok")
so this is not exactly my code but of a python tutorial on sololearn
it should output:
1
2
3
4
5
and then stop
but as i do this i get a loop of 1
1
1
1
1
1
which i believe is not corect
so where im getting is am i making a mistake while overtyping (wivh i have surely corrected) or is this code a wrong code wich has been wrongly used in the app
OR did the python version change so this doesnt work anymore
Fix your indentation. This happens when your i = i + 1 is not in the loop.
You are running this code, which runs infinitely and prints 1:
i = 1
while i <= 5:
print(i)
i=i + 1
print("ok")
but you want this:
i = 1
while i <= 5:
print(i)
i = i + 1
print("ok")
Also, always use four spaces to indent Python code. That way it's much more obvious when the indentation is wrong.
For some reason the line that increments i is not executing in the loop. There a few possible reasons for this:
Maybe the line isn't in your source code. Please confirm that the line is present.
Maybe the line is commented out. The comment character in a Python program is #. Ensure that that character does not appear anywhere on the line.
Maybe, and this seems to me to be the most likely, the line is not indented correctly. In Python, indentation is significant. The body of a loop must be indented further than the while line. The first line that is not indented marks the end of the loop. Ensure that your code doesn't look like this:
i = 1
while i <=5:
print (i)
i=i + 1
print ("ok")
You may be missing tabs and spaces. Please ensure that all of the indentation in your program is exclusively the tab character or exclusively the space character, and never a mix of the two. The consensus in the Python community is to always use spaces, never tabs.
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.
I am brand new to python. I have been working on the courses on Codecademy. I am also currently using Pydev / LiClipse.
In one of the first lessons on Codecademy it wants you to set the variable parrot to "Norwegian Blue". Then it wants you to print the length of parrot using the len string method. It is very simple, and I got the answer right away with:
parrot = "Norwegian Blue"
print len(parrot)
When I put the exact same code into LiClipse it returned:
SyntaxError: invalid syntax
It work in LiClipse when I changed it to:
print (len(parrot))
Can someone let me know why that worked in codecademy, but not in LiClipse, and why adding the parenthesis fixed it?
It sounds like Pydev/LiClipse is using Python 3 while Codeacademy is using python 2.x or some other older version. One of the changes made when python updated from 2.x to 3 was print is now a function.
Python 2:
print "stuff to be printed"
Python 3:
print("stuff to be printed")
You must take into account the version in which you are working.
In Python 2 your code would look like this:
parrot = "Norwegian Blue"
print len(parrot)
In Python 3 your code would look like this:
parrot = "Norwegian Blue"
print ( len(parrot) )
It worked in CodeAcademy because their interpreter is a Python 2.7, where you didn't need the parenthesis because print was a statement. In Python 3.0+, print requires the parentheses because it's a function.
More information on what's different between Python 2.7 and 3.0+ can be found here:
What's New In Python 3.0
Some of the sample differences with print on the above page:
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
It's good to know the differences between both, in case you're working with legacy systems and the lot vs. in your private environment. In Python 2.7 and below, print() works; however, omitting the ()s does not work in Python 3.0+, so it's better to get into the habit of using them for print.
End of life for Python 2.7 is expected to be in 2020, so you have plenty of time anyway.
In Python 3 print was changed to require parenthesis. CodeAcademy is probably using Python 2 and it looks like you're using Python 3.
https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
From the docs
Print Is A Function
The print statement has been replaced with a
print() function, with keyword arguments to replace most of the
special syntax of the old print statement (PEP 3105). Examples: