简体   繁体   English

打印功能输出

[英]print function output

In Python 3: 在Python 3中:

>>> str="Hello" # declare a string str
>>> print(str)
Hello
>>> str
'Hello'

When I don't use print to print the string and simply write >>> str then the output comes in single quotes and when I use >>> print(str) the output does not come in single quotes. 当我不使用print来打印字符串而只是写>>> str ,输出将以单引号引起来,而当我使用>>> print(str) ,输出将不会以单引号引起来。 So why does this happen? 那么为什么会这样呢?

In Python interactive shell: 在Python交互式shell中:
If you declare a variable, then when you type the name of that variable, Python interactive shell will return the value of it. 如果声明一个变量,那么当您键入该变量的名称时,Python交互式外壳程序将返回它的值。 Because there are many data types, if you declare a string, and then call it, Python interactive shell will display it with quotes around, so that you can know it is a string 因为数据类型很多,所以如果您声明一个字符串然后调用它,Python交互式外壳程序将在其周围加上引号,以便您可以知道它是一个字符串。

>>> str = "Hello"
>>> str
'Hello'

If you use the print() function to print the string out, then the string is being "printed". 如果使用print()函数将字符串打印出来,则说明正在“打印”字符串。 No quotes. 没有引号。 Nothing more, nothing less. 仅此而已。

When you simply write >>>str you are calling that variable, so the single quotes help to denote that it is a string, but using the print() function it prints the contents of str to the IDLE. 当您简单地编写>>> str时,您正在调用该变量,因此单引号有助于表明它是一个字符串,但是使用print()函数会将str的内容打印到IDLE。 Hope this helps :) 希望这可以帮助 :)

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

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