Why Is Parenthesis in Print Voluntary in Python 2.7

Why is parenthesis in print voluntary in Python 2.7?

In Python 2.x print is actually a special statement and not a function*.

This is also why it can't be used like: lambda x: print x

Note that (expr) does not create a Tuple (it results in expr), but , does. This likely results in the confusion between print (x) and print (x, y) in Python 2.7

(1)   # 1 -- no tuple Mister!
(1,) # (1,)
(1,2) # (1, 2)
1,2 # 1 2 -- no tuple and no parenthesis :) [See below for print caveat.]

However, since print is a special syntax statement/grammar construct in Python 2.x then, without the parenthesis, it treats the ,'s in a special manner - and does not create a Tuple. This special treatment of the print statement enables it to act differently if there is a trailing , or not.

Happy coding.


*This print behavior in Python 2 can be changed to that of Python 3:

from __future__ import print_function

Print not working without parenthesis (Python 2.7)

This is causing the problem: from __future__ import print_function.

This means that you are importing the new print_function from Python 3 into Python 2.7. Hence, parenthesis are required. More on future imports.

Python prints the parenthesis from the function

Your problem is you're using code written for python 3, where print is a function:

>>> import sys; sys.version_info.major
3
>>> print('a', 'b')
a b

but running it in python 2, where it is a statement:

>>> import sys; sys.version_info.major
2
>>> print ('a', 'b')
('a', 'b')

If you're writing code that you want to print the same way in both python 2 and python 3, you can use

from __future__ import print_function
print('a', 'b') # works as expected in both versions of python

2.7.6 python version needs parenthesis to print?

Your impression is correct, it's not needed (unless of course you import print_function von __future__!). However, it's not prohibited either. print is followed by an expression, and (sys.version) is a valid expression as much as sys.version is. Note that (x) does not create a tuple containing x (that would be (x,)).

Brackets on Print Statements in Python on Stack Overflow

Print statement in Python 2 was removed in favor of a print function in Python3. Functions are invoked using parentheses - "()", so they are actually needed.

print a function by default in Python 2.7.11?

Note that the string "a function?" is an expression in Python, and an expression in parentheses is also an expression.

So your command was print ("a function?"), just printing an expression.

That is convenient for writing a line that works in both Python 2.x and in Python 3.x. The book "Python Crash Course" uses this to show code that works in both versions of Python.

Why does print with and without parentheses give __str__() and __repr__() in

Even though it looks like you're making a function call, you are not. In python 2, print is a keyword. So your 2nd example is really:

print (source, destination)

i.e. you are printing a tuple. So, what you are getting is actually the str of the tuple, which shows the repr of its parts.

In recent versions of python, you can get the print function in python 2 for compatibility, by using

from __future__ import print_function

Then this will do what you expected (but your first example becomes invalid).

Why does this regex code have different results in Python 2.7 and Python 3.7?

It's because of the different definitions for \w in Python 2.7 versus Python 3.7.

In Python 2.7, we have:

When the LOCALE and UNICODE flags are not specified, matches any
alphanumeric character and the underscore; this is equivalent to the
set [a-zA-Z0-9_]
.

(emphasis and hyperlink and formatting added)

However, in Python 3.7, we have:

For Unicode (str) patterns: Matches Unicode word characters; this
includes most characters that can be part of a word in any language,
as well as numbers and the underscore
. If the ASCII flag is used, only
[a-zA-Z0-9_] is matched.

(emphasis and formatting added)

So, if you want it to work in both versions, you can do something like this:

# -*- coding: utf-8 -*-
import re
regex = re.compile(r'^[\w.@+-]+\Z', re.UNICODE)
match = regex.match(u'名字')

if match:
print(match.group(0))
else:
print("not matched!")

output:
名字

Here's proof that it works in both versions:

works

Note the differences:

  • I added # -*- coding: utf-8 -*- at the top of the script, because without it, in Python 2.7, we'll get an error saying

    Non-ASCII character '\xe5' on line 3, but no encoding declared; see
    http://www.python.org/peps/pep-0263.html for details

  • Instead of using result = re.match(pattern, string), I used regex = re.compile(pattern, flags) and match = regex.match(string) so that I can specify flags.

  • I used re.UNICODE flag, because without it, in Python 2.7, it will only match [a-zA-Z0-9_] when using \w.

  • I used u'名字' instead of '名字', because in Python 2.7 you need to use Unicode Literals for unicode characters.

Also, while answering your question, I found out that print("not matched!") works in Python 2.7 as well, which makes sense, because in this case the parentheses are ignored, which I didn't know, so that was fun.

Removing brackets and quotes from print in Python 2.7

re.findall returns a list. When you do str(someList), it will print the brackets and commas:

>>> l = ["a", "b", "c"]
>>> print str(l)
['a', 'b', 'c']

If you want to print without [ and ,, use join:

>>> print ' '.join(l)
a b c

If you want to print without [ but with ,:

>>> print ', '.join(l)
a, b, c

And if you want to keep the ', you could use repr and list comprehension:

>>> print ', '.join(repr(i) for i in l)
'a', 'b', 'c'

After your edit, it seems that there is only 1 element in your lists. So you can only print the first element:

>>> l = ['a']
>>> print l
['a']
>>> print l[0]
a


Related Topics



Leave a reply



Submit