How to fix print(( (double parentheses) after 2to3 conversion?

python code converter
convert python print statements
python print without parentheses
porting python 2 to 3
python print output without parentheses
converting py2 to py3
python 3 to python 2 converter
python double parentheses

When migrating my project to Python 3 (2to3-3.7 -w -f print *), I observed that a lot of (but not all) print statements became print((...)), so these statements now print tuples instead of performing the expected behavior. I gather that if I'd used -p, I'd be in a better place right now because from __future__ import print_function is at the top of every affected module.

I'm thinking about trying to use sed to fix this, but before I break my teeth on that, I thought I'd see if anyone else has dealt with this before. Is there a 2to3 feature to clean this up?

I do use version control (git) and have commits immediately before and after (as well as the .bak files 2to3 creates), but I'm not sure how to isolate the changes I've made from the print situations.

You were already printing tuples. If you were not, then you are not now either.

To illustrate, your code would have to have been using print as if it was a function:

# Python 2
print(somestring)

which becomes, after translation

# Python 3
print((somestring))

That's not a tuple, that's just a pair of parentheses. It results in the same output on either version. In fact, 2to3 is smart enough to drop the inner parentheses again; the actual output written is simply

# Python 3
print(somestring)

However, if you were using multiple arguments in Python 2:

# Python 2
print(arg1, arg2)

then you'd already be printing a tuple, because that's really:

value_to_print = (arg1, arg2)
print value_to_print

So it would only be correct to preserve that behaviour in Python 3. If you see the 2to3 tool use print((....)), then it determined that you were already printing tuples.

Demo:

$ cat testprint.py
print('not a tuple')
print('two-value', 'tuple')
$ python2.7 testprint.py
not a tuple
('two-value', 'tuple')
$ 2to3 -w -f print testprint.py
RefactoringTool: Refactored testprint.py
--- testprint.py    (original)
+++ testprint.py    (refactored)
@@ -1,2 +1,2 @@
 print('not a tuple')
-print('two-value', 'tuple')
+print(('two-value', 'tuple'))
RefactoringTool: Files that were modified:
RefactoringTool: testprint.py
$ python3.7 testprint.py
not a tuple
('two-value', 'tuple')

Note that this is different from using from __future__ import print_function in your Python 2 code, to disable the print statement and so making the code call the built-in print() function. The 2to3 tool already detects this case and will pass through print(...) function calls unchanged:

$ cat futureprint.py
from __future__ import print_function
print('not', 'a tuple')
$ python2.7 futureprint.py
not a tuple
$ 2to3 -w -f print futureprint.py
RefactoringTool: No files need to be modified.
$ python3.7 futureprint.py
not a tuple

You can force 2to3 to assume all your files use from __future__ import print_function, regardless, with the -p / --print-function command-line switch:

-p, --print-function  Modify the grammar so that print() is a function

However, any deliberate print (tuple_element1, tuple_element2, ...) print statements would then be mis-translated as function calls:

$ cat printtuple.py
print ('two-value', 'tuple')
$ python2.7 printtuple.py
('two-value', 'tuple')
$ 2to3 -w -f print -p printtuple.py
RefactoringTool: No files need to be modified.
$ python3.7 printtuple.py
two-value tuple

Double parenthesis in print function running 2to3 in already correct , If 2to3 is run on a file with the following contents: a="string" print print (("%s" % a)) Although it was already a valid call to print in python3. 2to3 is designed as a tool to convert a Python 2 code base to Python 3 at once. When the -p is passed, 2to3 treats print as a function instead of a statement. This is useful when from __future__ import print_function is being used. If this option is not given, the print fixer will surround print calls in an extra set of parentheses because it cannot differentiate between the print statement with parentheses (such as print

If your code already has print() functions you can use the -x print argument to 2to3 to skip the conversion.

On Windows10, how come 2to3 utility produces extra parenthesis , print ("In the test.py program") print(("Using {} to find version".format('sys'))) 2to3 -p test.py.bak RefactoringTool: Skipping optional fixer: buffer RefactoringTool: RefactoringTool: There was 1 error: RefactoringTool: Can't parse test.py.bak: anything in a py2 print statement in brackets to make it look like a function for py3. 8 How to fix print(( (double parentheses) after 2to3 conversion? Apr 7 '19. 7 Converting long integers to strings in pandas (to avoid scientific notation) Oct 27 '14.

Try using the -p flag. See the last note here.

When the -p is passed, 2to3 treats print as a function instead of a statement. This is useful when from __future__ import print_function is being used. If this option is not given, the print fixer will surround print calls in an extra set of parentheses because it cannot differentiate between the print statement with parentheses (such as print ("a" + "b" + "c")) and a true function call.

The biggest mistake Python 3 made was the print statement change , Just deprecating the old syntax and giving tons of warnings for a "When using the 2to3 source-to-source conversion tool, all print Python3 allowed the devs to fix some of the mistakes or sins of the past, similarly `input`/`raw_input`. In python, functions are called using parentheses, so "make print a� 2to3 is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. The standard library contains a rich set of fixers that will handle almost all code. 2to3 supporting library lib2to3 is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3.

Python parentheses primer — Reuven Lerner, Perhaps the most obvious use for parentheses in Python is for calling d = {'a':1, 'b':2, 'c':3} for key, value in d.items: print(f"{key}: {value}"). When you try to run this code, you can an error message that is true, but In [21]: '*'.join((str(x) . Notice the double parentheses here; the outer ones are for the call to� 2to3 is a Python program that reads Python 2.x source code and applies a series of fixers to transform it into valid Python 3.x code. The standard library contains a rich set of fixers that will handle almost all code. 2to3 supporting library lib2to3 is, however, a flexible and generic library, so it is possible to write your own fixers for 2to3.

Python print parentheses in string, Print all the valid parentheses combinations for the given number. to convert a Python list (or any other iterable such as a tuple) to a string for display. We can use either single or double quotes for strings – Python will happily accept either. and/or curly braces. x as well: Formatted Output in Python 2. zeros((rows,cols) � Case study: porting chardet to Python 3 describes how to run the 2to3 script, then shows some things it can’t fix automatically. This appendix documents what it can fix automatically. print statement. In Python 2, print was a statement. Whatever you wanted to print simply followed the print keyword. In Python 3, print() is a function.

What has to be true about the pairs of numbers inside the , Here, we are going to learn how to check for balanced parentheses by using stack using multiplication chart (for positive exponents) or a decimal conversion chart (for Proofread the paragraphs to correct the punctuation, capitalization, and duplicate parenthesis as shown below: ((a+b)) (((a+(b)))+(c +d)) Write code to� The fix_print_with_import fixer in libfuturize.fixes changes the code to use print as a function and also adds from __future__ import print_function to the top of modules using print(). In addition, it avoids adding an extra set of parentheses if these already exist. So print(x) does not become print((x)).

Comments
  • Thanks. This is good to know. I made a bunch of edits to the transformed files before realizing the mistake though, so I'm looking for a way to correct the situation without reverting to the pre-migration state.
  • @HaPsantran: that's where you really need to be using a version control system. Commit your working code, translate and test, commit again. Preferably on a branch if you are not certain of your tests.
  • @HaPsantran: otherwise, using sed or another regex tool can easily lead to false-positives and errors. You could take a look at the existing print fixer in 2to3 itself, and base your own fixer off of that. See python3porting.com/fixers.html
  • Thanks for yet another great post. One question though: Why does 2to3 convert e.g. print('{}'.format(0)) to print(('{}'.format(0)))?
  • @djvg: because in Python 2, print('{}'.format(0)) is really value_to_print = ('{}'.format(0)), print value_to_print. That's not an issue when there is just one value, but now compare this to print(42, 81), which prints a tuple of two values. Plus, 2to3 tries to preserve the use of parentheses as much as possible to minimize changes (in case you were using the parentheses for readability, e.g. value = (81 + 42) - 17).
  • Thanks; I think I needed that before running 2to3 though. At this point, I've cleaned up everything manually.