>>> print "hello world"
hello world
>>> "hello world"
'hello world'
>>> 有什么关系?
一个完完全全的菜鸟问题
python hello world示例主要使用
print "hello world"我可以去掉这个print,只使用"Hello world"来介绍python吗?
发布于 2013-12-11 12:37:37
不同之处在于print调用str,而REPL (read evaluate print循环)的默认操作是在对象上调用repr,除非它是None。
请注意,如果您没有在交互式解释器中工作(您不在REPL中),那么在没有print的版本中将看不到任何输出。
另请注意,输出之间存在差异。repr在字符串上添加引号。
发布于 2013-12-11 12:50:31
如果您将空格替换为换行符,您将看到它们在REPL中的工作方式并不相同。
>>> print "hello\nworld"
hello
world
>>> "hello\nworld"
'hello\nworld'如果您尝试使用
"hello\nworld"当然,在程序中,你不会得到任何输出
发布于 2015-07-18 14:04:52
如果你知道一点Bash脚本,你会发现在echo中' $HOME‘会打印用户字母,而不是$HOME中的实际值,我指的是/$HOME/$HOME/。
因此,对于Python,print函数将被解释为双引号和单引号,这是相同的。如果没有引号或句子不需要使用双引号,我只使用"“。尽管如此,您仍然可以使用\“。
示例:
print 'hello\nworld' -> hello
world
print "hello\nworld" -> hello
world和
'hello\nworld' -> hello\nworld如果有时,我需要原始输出,我不会使用打印或尝试其他解决方法。
另外,如果我必须使用print函数来输出原始字符串。我只是用这个-
print repr("hello\n\tworld\\") -> 'hello\n\tworld\\'不过,这取决于你的情况。
请随时编辑/建议更好地改进此回复。在这个答案中,我可能会犯很多错误和错误的信息。
https://stackoverflow.com/questions/20510585
复制相似问题