简体   繁体   English

在python解释器中,为什么使用显式“ print”与不使用“ print”之间的结果有所不同?

[英]In python interpreters, why are results different between using an explicit “print” and not using it?

>>> 'sp\xc4m'
'sp\xc4m'
>>> print('sp\xc4m')
spÄm

( screenshot ) 截图

In IDLE,I think that using the function "print" and not using it should be the same. 在IDLE中,我认为使用“打印”功能与不使用它应该是相同的。 And the book "Learning Python(5th)" supports my opinion. 而“学习Python(第5本书)”这本书支持了我的观点。

>>> lumberjack = 'okay'
>>> lumberjack
'okay'

example from book 书中的例子

I am a freshman in python, and my mother language isn't English, so maybe my question and expression are funny, but I keep learning. 我是python的新生,我的母语不是英语,所以我的问题和表达方式很有趣,但我一直在学习。 Thank you for your help. 谢谢您的帮助。

IDLE prints the repr of a string (or anything else). IDLE显示字符串的repr (或其他任何形式)。

>>> print 'sp\xc4m'
sp?m
>>> 'sp\xc4m'
'sp\xc4m'
>>> print repr('sp\xc4m')
'sp\xc4m'
>>> 

The interactive interpreter in IDLE is helpful for debugging. IDLE中的交互式解释器有助于调试。 If I wrote: 如果我写了:

>>> print s
a       b

What is actually contained in the variable s ? 变量s实际包含什么? You can check: 您可以检查:

>>> s
'a\tb'

Now you can see that it is three characters, a , a tab displayed as the escape sequence \\t , and b . 现在您可以看到它是三个字符, a ,一个显示为转义序列\\t的选项卡, b It could have been 'a b' (nine characters with spaces) or `'ab ' (twelve characters with trailing spaces). 可能是'a b' (带空格的九个字符)或''ab'(带空格的十二个字符)。 Both print the same way, but the debugging representation helps determine the exact content via escape codes and quoting the string to visualize the start and end points. 两者都以相同的方式打印,但是调试表示形式可以通过转义码并引用字符串来确定起点和终点,从而确定确切的内容。

This is called the "representation" of the variable, and is a debug value that can be accessed by the function repr() . 这称为变量的“表示形式”,它是可以由函数repr()访问的调试值。 IDLE uses it to display variables that weren't explicitly printed. IDLE使用它来显示未明确打印的变量。

At the interactive prompt, displaying a variable without print is for debugging. 在交互式提示下,显示不print的变量用于调试。 Not using print in a script would display nothing. 不在脚本中使用print不会显示任何内容。 To force display of the debugging value in a script, use print repr(s) . 要在脚本中强制显示调试值,请使用print repr(s)

The interactive interpreter's auto-print behavior is equivalent to 交互式解释器的自动打印行为等效于

_ = the_expression_you_typed
if _ is not None:
    print repr(_)

Besides the None thing, this differs from print _ in that print _ would call str instead of repr . 除了None以外,这与print _不同之处在于print _将调用str而不是repr Ostensibly, the difference between str and repr is that str is supposed to produce more human-readable output, while repr is supposed to produce something unambiguous, ideally a piece of source code that would produce the object. 从表面上看, strrepr之间的区别是str应该产生更多的人类可读输出,而repr应该产生明确的东西,理想情况下是可以产生对象的源代码。

Really, though, the main reason the two functions exist is so print "asdf" prints asdf and just typing "asdf" prints "asdf" . 但是,实际上,存在这两个函数的主要原因是,所以print "asdf"打印asdf而仅键入"asdf"打印"asdf"

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

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