简体   繁体   English

python 打印“你好世界”与“你好世界”

[英]python print "hello world" vs "hello world"

>>> print "hello world"
hello world
>>> "hello world"
'hello world'
>>> 

What is the difference?有什么区别?

A complete noob question.一个完整的菜鸟问题。

python hello world example mostly uses python hello world 示例主要使用

print "hello world"

Can I strip that print and just use "Hello world" for giving a python introduction?我可以去掉那个print并只使用"Hello world"来介绍 python 吗?

The difference is that print calls str whereas the default action of the REPL (read evaluate print loop) is to call repr on the object unless it is None .不同之处在于print调用str而 REPL(读取评估打印循环)的默认操作是在对象上调用repr ,除非它是None

Note that if you aren't working in the interactive interpreter (you're not in the REPL), then you won't see any output in the version without print .请注意,如果您不在交互式解释器中工作(您不在 REPL 中),那么您将不会在没有print的版本中看到任何输出。

Also note that there is a difference between the output.另请注意,输出之间存在差异。 repr adds quotes on strings. repr在字符串上添加引号。

If you substitute the space for a newline, you'll see they don't even really work the same in the REPL.如果你用空格代替换行符,你会发现它们在 REPL 中的工作方式甚至不一样。

>>> print "hello\nworld"
hello
world
>>> "hello\nworld"
'hello\nworld'

If you try to use如果您尝试使用

"hello\nworld"

by itself in a program, you will get no output of course在一个程序中,你当然不会得到任何输出

If you know a bit of Bash scripting, you'll found by echo '$HOME' will print $HOME letter and not the actual value in $HOME, by that I mean /home/user/.如果您了解一些 Bash 脚本,您会发现echo '$HOME'将打印 $HOME 字母而不是 $HOME 中的实际值,我的意思是 /home/user/。

So as Python, however, print function will interpreted as double quotes and single quote as the same thing.然而,作为 Python,print 函数将解释为双引号和单引号是同一个东西。 I just use "" if there is no quotes or a sentence that don't have to use double quotes.如果没有引号或不必使用双引号的句子,我只使用 ""。 Although, you still can do that with \\".尽管如此,您仍然可以使用 \\" 来做到这一点。

Example -例子 -

print 'hello\nworld' -> hello
                        world

print "hello\nworld" -> hello
                        world

and

'hello\nworld' -> hello\nworld

If sometimes, I need raw output, I will not use print or try other workarounds.如果有时,我需要原始输出,我不会使用打印或尝试其他解决方法。

Also, if I have to use print function to output raw strings.另外,如果我必须使用打印函数来输出原始字符串。 I just use this -我只是用这个 -

print repr("hello\n\tworld\\") -> 'hello\n\tworld\\'

It depends on your condition, though.不过也要看你的情况。

Feel free to edit/suggest for better improvements of this reply.随意编辑/建议更好地改进此回复。 I may do a lot of mistakes and misinformations in this answer.在这个答案中,我可能会犯很多错误和错误信息。

This is a very good question, in fact, the same doubt I had while I started to learn Python.这是一个很好的问题,事实上,我在开始学习 Python 时也有同样的疑问。 First, we'll observe the different outputs of these two different programs.首先,我们将观察这两个不同程序的不同输出。

Program type 1程序类型 1

>>> print "hello world"
hello world

The programs using print function, as we know is used to print the given string or numeric data.众所周知,使用打印功能的程序用于打印给定的字符串或数字数据。

Program type 2程序类型 2

>>> "hello world"
'hello world'
>>> 'hello world'
'hello world'

When the print function is not used, and the string is directly told to be printed by giving it within single/double quotes, the answer also gets printed within quotes.当不使用打印函数时,通过在单/双引号内给出字符串直接被告知要打印,答案也会在引号内打印。 We don't need this type of outputs for our programs, even though it might be easy to not use the print command.我们的程序不需要这种类型的输出,即使不使用打印命令可能很容易。

This is possible only in Python Command Line.这仅在 Python 命令行中是可能的。 It is not possible if the program is saved in a separate file and will be executed totally.如果程序保存在单独的文件中并且将完全执行,则不可能。

You are running on python interactive shell.您正在 python 交互式 shell 上运行。

print "hello world" You have executed a function call that print function executed and output the string you given to the stream file which default is stdout https://docs.python.org/3/library/functions.html#print print "hello world"您已经执行了一个函数调用,该函数调用执行了print函数并将您提供给流文件的字符串输出,该字符串默认为stdout https://docs.python.org/3/library/functions.html#print

>>> "hello world" Is just let python interpreter excute a line of code which is only a string. >>> "hello world"只是让python解释器执行一行代码,它只是一个字符串。 Since you are running on python interactive shell.由于您在 python 交互式 shell 上运行。 The shell will display the string "hello world" to the screen. shell 将在屏幕上显示字符串“hello world”。

It is the fact that you are using python shell that it does anything at all.事实上,您使用的是 python shell,它可以做任何事情。 to prove it, if you actually use a python file, it does nothing at all为了证明这一点,如果你真的使用了一个 python 文件,它什么都不做

 Python 3.7.7 (tags/v3.7.7:d7c567b08f, Mar 10 2020, 10:41:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
==== RESTART: C:/Users/s-wan/AppData/Local/Programs/Python/Python37/testhelloworld.py ===
>>> 

but if you actually use it in shell, then it prints the thing out with single quotes:但是如果你真的在 shell 中使用它,那么它会用单引号打印出来:

>>> "Hello World"
'Hello World'

using使用

print('hello world')

will print 'hello world' without quotes.将打印不带引号的 'hello world'。 the shell and the actual file can produce different results. shell 和实际文件会产生不同的结果。

wow...哇...

>>> print("hello\nworld")

hello world你好世界

"hello\\nworld" 'hello\\nworld' 'hello\\nworld' 'hello\\nworld' "hello\\nworld" 'hello\\nworld' 'hello\\nworld' 'hello\\nworld'

只有在命令行中才能避免使用打印功能,否则必须使用打印功能

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

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