Difference between print string with parenthesis and without [duplicate] - python

This question already has answers here:
What is the advantage of the new print function in Python 3.x over the Python 2 print statement?
(5 answers)
Closed 4 years ago.
In Python 2.7 (interactive mode), both:
print("Hey Joe")
and:
print "Hey Joe"
give output:
"Hey Joe"
What is the difference? When should I use the former and when the latter?
Thank you

What is the difference?
Generally print 'something' is called print statement and print("something") is called print function. Print function has been introduced with Python 3. Other than that looking at the basic usage you won't notice any difference. However, you could find more here.
When should I use the former and when the latter?
If you want to make your code both Python 2.7 and Python 3 compliant than you should use print function, it is safe to import it with Python 2 and Python 3 it makes difference only with Python 2.
from __future__ import print_function

Related

print type(x) SyntaxError: invalid syntax [duplicate]

This question already has answers here:
Python 3 print without parenthesis
(10 answers)
Closed 4 years ago.
So I started learning python 3 and I wanted to run a very simple code on ubuntu:
print type("Hello World")
^
SyntaxError: invalid syntax
When I tried to compile that with command python3 hello.py in terminal it gave me the error above, but when used python hello.py (I think it means to use python 2 instead of 3) then it's all fine. Same when using python 3 and 2 shells in the terminal.
It seems like I'm missing something really stupid because I did some research and found no one with the same issue.
In Python3, print was changed from a statement to a function (with brackets):
i.e.
# In Python 2.x
print type("Hello World")
# In Python 3.x
print(type("Hello World"))
In Python 3.x print() is a function, while in 2.x it was a statement. The correct syntax in Python 3 would be:
print(type("Hello World"))
This is because from Python 3, print is a function, not a statement anymore. Therefore, Python 3 only accepts:
print(type("Hello World"))

Print statement as they are called in a python script automatically when the script runs [duplicate]

This question already has an answer here:
How can I decorate all functions imported from a file?
(1 answer)
Closed 4 years ago.
I'd like to automatically print python statements as they are called in a script while the script runs. Could anybody show a ready-to-use solution?
For example, I may have the following python code.
print "xxx"
myfun()
I'd like print "xxx" and myfun() be printed exactly in the output.
One solution is manually to add a print statement to each function. But this is combersome.
print 'print "xxx"'
print "xxx"
print 'myfun()'
myfun()
The following solutions just can not do so. Please do not mark this question as duplicate.
The solution here will not only print the functions in the current script but also functions in other scripts used in the current scripts. These solutions are not what I need.
How do I print functions as they are called
The following does not do what I need because it does not limit the decoration to the current file. For example, it uses import mymod1; decorate_all_in_module(mymod1, decorator). I don't have a module. I want to print the statements in the current script.
How can I decorate all functions imported from a file?
There is no "ready-to-use" solution to this problem. You are asking for a programming language with different semantics than the Python language: therefore you will either need to write code to get this effect, or use a different language, or both.
That said, for solving your actual problem, I believe simply using a debugger would do what you want: that way you can step over each statement in your program and observe as they are executed.

Python SyntaxError? [duplicate]

This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 6 years ago.
I am learning a tutorial for Python language and trying to write a basic "Hello World!" program.
But when I do all steps described in the book I receive an error.
>> print "Hello World!"
SyntaxError: Missing parentheses in call to 'print'
Why I am getting this error?
Is my book wrong?
it seems that you're using Python 3.x.
In python 3.x, the print statement is a function and you need to use it as a function like this
print("Hello World!")
Your book is right, but might be outdated a bit. It seems it describes Python version 2, but you try to run your example on the version 3.
Python 3 has changed some features and this one is the most annoying to switch from P2 to P3.
"print" statement changed to function rather than operator as was in P2.
Calling function you should always use parentheses.
So, if you want to run your program in Python3 you should call it:
print("Hello World!")
And that's it.
If you want to use examples from your book as is - install Python2 and it should work.

How to print in the Python programming language [duplicate]

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 7 years ago.
I am a new learner to the Python language. When I want to print, I use the syntax:
print "hello world"
But when I use this syntax, it shows an error like missing parenthesis in calling print. I don't know why it is happening, because I know there is no need of using parentheses in Python to print a string like
print ("hello world")
What wrong have I done?
This is the error I get:
SyntaxError: Missing parentheses in call to 'print'
Consider:
print "hello world"
The above statement is OK when you are using Python 2.x, because in Python 2.x, print is a statement.
But in Python 3.x, print is a function and there is no way to turn it back into a statement. So you must use parentheses.
So for Python 3.x, the answer is:
print("hello world")
Please refer to: Print Is A Function.

What is this print syntax? (print rightshift) [duplicate]

This question already has an answer here:
How does the right-shift operator work in a python print statement?
(1 answer)
Closed 6 months ago.
Looking at the source code of pstats I see this syntax:
print >> self.stream, "in %.3f seconds" % self.total_tt
print >> self.stream
What is this syntax, how is it called and how to use it? I have never seen it before, nor seen it in any of the Python books/tutorials I have read.
If you mean the >>, that's the Python 2.x syntax for writing to a file-like other than sys.stdout with print. It's defined in the Python docs, 6.6. The print statement and has been around since at least Python 2.5 (and I think earlier).
This syntax has been replaced with a file kwarg to the print function in Python 3.0.
It's the extended form of the print statement which redirects the output to the file-like object immediately following it. See the Python docs.

Categories

Resources