简体   繁体   中英

Python 2.7.9 Print statement with list strange output

Why whole argument in print function along with paranthesis is printed when only the string should have been

This is Python 2.7.9

import os

alist = [ 'A' ,'B']

print('Hello there')
print('The first item is ',alist[0])
print('Good Evening')

root@justin:/python# python hello.py
Hello there
('The first item is ', 'A')
Good Evening

In python 2 print isn't a function it's a statement. When you write

print('The first item is ',alist[0])

it's actually means "print me a tuple of 2 elements: 'The first item is ' and alist[0] "

it's equivalent to

a = ('The first item is ',alist[0])
print a

if you want to print only strings you should remove the parentheses like that:

print 'The first item is ',alist[0]

EDIT: As guys in comments tell, you can also add

from __future__ import  print_statement

This will make print a function like in python 3 and your examples will work as you expected without any changes.

But I think it's useful to understand what is going on in both cases.

Remember You're using python 2.7.x in python 2, print is a statement, not a function. You might ask why

print('Good Evening')

doesn't print

('Good Evening')

You're passing only 1 string argument hence the print statement understands that the string needs to be printed and not the parentheses.

when you do

print ('The first item is ',alist[0])

The whole output is printed thinking that there are different parts of the string having , as the delimiter, hence the output is

('The first item is ', 'A')

Remove parentheses while dealing with python 2 because it is not a function oriented version

Earlier answers have explained that

print('The first item is ', alist[0])

in Python 2 is equivalent to

print ('The first item is ', alist[0])

so it prints a tuple of two items. That's because print is a statement in Python 2, not a function, so parentheses following print are not interpreted as indicating a function call.

In Python, an expression consisting of several items separated by commas creates a tuple. In some cases, parentheses are required to group the tuple into a single unit; and the print statement is one of those cases, otherwise each item in the comma-separated sequence is treated as a separate argument to the print statement.

The standard string representation of a tuple prints the enclosing parentheses and the repr of each tuple item. Thus any string items in the tuple are printed with their quote marks, and various escape sequences are used to represent non-ASCII characters.

If you wish to use the print() syntax in latter versions of Python 2 in order to make your code compatible with Python 3 then you should put

from __future__ import print_function

as the first executable statement in your script. That will mask the print statement and allow the print name to refer to the print function instead. Here's a short demo, running on Python 2.6.6. First, without the import:

print('one', 'two\n', 'three')    

output

('one', 'two\n', 'three')

And with the import:

from __future__ import print_function
print('one', 'two\n', 'three')

output

one two
 three

FWIW, you might as well do

from __future__ import print_function, division

So you get Python 3-style behaviour of the / division operator too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM