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?

Upvotes: 0

Views: 85

Answers (2)

First Last
First Last

Reputation: 200

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.

Upvotes: 1

chngzm
chngzm

Reputation: 628

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 :)

Upvotes: 0

Related Questions