No, you cannot. The print statement is gone in Python 3; the compiler doesn't support it anymore.
You can make print() work like a function in Python 2; put this at the top of every module that uses print:
from __future__ import print_function
This will remove support for the print statement in Python 2 just like it is gone in Python 3, and you can use the print() function that ships with Python 2.
six can only help bridge code written with both Python 2 and 3 in mind; that includes replacing print statements with print() functions first.
You probably want to read the Porting Python 2 Code to Python 3 howto; it'll tell you about more such from __future__ imports as well, as well as introduce tools such as Modernize and Futurize that can help automate fixing Python 2 code to work on both Python 2 and 3.
You can use the tool 2to3 is an Automated Python 2 to 3 code translation, as @Martijn Pietersโฆ told :), you can get over a ride throw the old python and making the changes work into python 3, I make a simple example like this:
I created this file, python2.py:
#!python3
print 5
when I run it with python it obviously shows:
line 3
print 5 ^
SyntaxError: Missing parentheses in call to 'print'
so, you can transform it via terminal like this:
This is the important comand
$ 2to3 -w home/path_to_file/python2.py
-w parameter will write the file, if you want only see the future changes without apply them, just run it without -w. after run it it will show something like
root: Generating grammar tables from /usr/lib/python2.7/lib2to3/PatternGrammar.txt
RefactoringTool: Refactored Desktop/stackoverflow/goto.py
--- Desktop/stackoverflow/goto.py (original)
+++ Desktop/stackoverflow/goto.py (refactored)
@@ -1,3 +1,3 @@
#!python3
-print 5
+print(5)
RefactoringTool: Files that were modified:
And the file will look like:
#!python3
print(5)
In Python2, print was a keyword which introduced a statement:
print "Hi"
In Python3, print is a function which may be invoked:
print ("Hi")
In both versions, % is an operator which requires a string on the left-hand side and a value or a tuple of values or a mapping object (like dict) on the right-hand side.
So, your line ought to look like this:
print("a=%d,b=%d" % (f(x,n),g(x,n)))
Also, the recommendation for Python3 and newer is to use {}-style formatting instead of %-style formatting:
print('a={:d}, b={:d}'.format(f(x,n),g(x,n)))
Python 3.6 introduces yet another string-formatting paradigm: f-strings.
print(f'a={f(x,n):d}, b={g(x,n):d}')
The most recommended way to do is to use format method. Read more about it here
a, b = 1, 2
print("a={0},b={1}".format(a, b))
The syntax has changed in that print is now a function. This means that the % formatting needs to be done inside the parenthesis:1
print("%d. %s appears %d times." % (i, key, wordBank[key]))
However, since you are using Python 3.x., you should actually be using the newer str.format method:
print("{}. {} appears {} times.".format(i, key, wordBank[key]))
Though % formatting is not officially deprecated (yet), it is discouraged in favor of str.format and will most likely be removed from the language in a coming version (Python 4 maybe?).
1Just a minor note: %d is the format specifier for integers, not %s.
Version 3.6+: Use a formatted string literal, f-string for short
print(f"{i}. {key} appears {wordBank[key]} times.")