Page 57 - Python Basics: A Practical Introduction to Python 3
P. 57
3.4. Inspect Values in the Interactive Window
Now print the string assigned to greeting using the print() function:
>>> print(greeting)
Hello, World
Can you spot the difference between the output displayed by using
print() and the output displayed by just entering the variable name
and pressing Enter ?
When you type the variable name greeting and press Enter , Python
prints the value assigned to the variable as it appears in your code.
You assigned the string literal "Hello, World" to greeting, which is why
'Hello, World' is displayed with quotation marks.
Note
String literals can be created with single or double quotation
marks in Python. At Real Python, we use double quotes wher-
ever possible, whereas IDLE output appears in single quotes by
default.
Both "Hello, World" and 'Hello, World' mean the same thing in
Python—what’s most important is that you be consistent in your
usage. You’ll learn more about strings in chapter 4.
On the other hand, print() displays a more human-readable represen-
tation of the variable’s value which, for string literals, means display-
ing the text without quotation marks.
Sometimes, both printing and inspecting a variable produce the same
output:
>>> x = 2
>>> x
2
>>> print(x)
2
56