简体   繁体   中英

print function output

In 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. So why does this happen?

In Python interactive shell:
If you declare a variable, then when you type the name of that variable, Python interactive shell will return the value of it. 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

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

If you use the print() function to print the string out, then the string is being "printed". 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. Hope this helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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