简体   繁体   English

Python 2.7.9打印语句,列表奇怪输出

[英]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 为什么只有字符串应该打印时才会打印打印功能中的整个参数以及paranthesis

This is Python 2.7.9 这是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. 在python 2中, print不是一个函数,它是一个声明。 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] " 它实际上意味着“给我打印2个元素的元组: 'The first item is '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. 这将使print成为python 3中的函数,您的示例将按预期工作而不进行任何更改。

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. 记住你在python 2中使用python 2.7.x ,print是一个语句,而不是一个函数。 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. 您只传递1个字符串参数,因此print语句理解需要打印字符串而不是括号。

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 在处理python 2时删除括号,因为它不是面向函数的版本

Earlier answers have explained that 早期的答案已经解释了这一点

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

in Python 2 is equivalent to 在Python 2中相当于

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. 那是因为print是Python 2中的一个语句 ,而不是一个函数,所以print括号不会被解释为表示函数调用。

In Python, an expression consisting of several items separated by commas creates a tuple. 在Python中,由逗号分隔的几个项组成的表达式创建一个元组。 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. 并且print语句是这些情况之一,否则逗号分​​隔序列中的每个项都被视为print语句的单独参数。

The standard string representation of a tuple prints the enclosing parentheses and the repr of each tuple item. 元组的标准字符串表示形式打印括号括号和每个元组项的repr Thus any string items in the tuple are printed with their quote marks, and various escape sequences are used to represent non-ASCII characters. 因此,元组中的任何字符串项都使用其引号打印,并且各种转义序列用于表示非ASCII字符。

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 如果您希望在后续版本的Python 2中使用print()语法以使您的代码与Python 3兼容,那么您应该放置

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. 这将掩盖print语句并允许print名称引用print功能。 Here's a short demo, running on Python 2.6.6. 这是一个在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 FWIW,你不妨这样做

from __future__ import print_function, division

So you get Python 3-style behaviour of the / division operator too. 所以你也得到了/ division运算符的Python 3风格行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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