You may have noticed that the text inside the parentheses is also between single or double quotes. It may not be clear why this is. In Python, putting text inside quotes designates it as a string.
The quotes are necessary to differentiate between code being fed into the print statement and the text to be printed.
If you want to print single quotes ('
) in your text you can do this by enclosing the text with double quotes (" "
) or vice versa. Python does not care if you use single or double quotes to enclose your strings as long as you are consistent.
We will illustrate this in the next exercise:
# string_quotes.py
print("This is a string:")
print("with'double' quotes but 'single quotes' inside it.")
print('with "single" quotes but "double quotes" inside it.')
print("mixing "double" and'single' quotes will cause an error.")
(If needed, see Section 4 to review how to run a script.)
$ python string_quotes.py
File "string_quotes.py", line 6
print("mixing "double" and'single' quotes will cause an error.")
^
SyntaxError: invalid syntax
$
If we comment out the last line,
# string_quotes.py
print("This is a string:")
print("with'double' quotes but 'single quotes' inside it.")
print('with "single" quotes but "double quotes" inside it.')
#print("mixing "double" and'single' quotes will cause an error.")
we will get:
$ python string_quotes.py
This is a string:
with'double' quotes but 'single quotes' inside it.
with "single" quotes but "double quotes" inside it.
$
When ever you make a string by using quotes always remember to be consistent with your use of single and double quotes or you will run into problems.
print
functionLet’s talk about the print()
statement or, as it is more correctly called, the print function.
print
is what is known as a built-in function in Python. Let’s get some terminology out of the way before moving on:
We will learn a lot more about functions later on and you will even build your own. But for now, just understand the following:
print
in the Python source code.print(stuff)
in your code you are telling the computer, “Activate that chunk of code labeled print
!”stuff
between the parentheses in the terminal window!”When we talk about functions you will see that they have things (such as stuff
above) between the parentheses. Sometimes these things are in a comma-separated list. These things between the parentheses are called arguments. For now, we have just one argument being fed to the print
function and that is simply the text you want printed out to the screen.
Remember: It is not required to figure out everything in the “Advanced Mastery” sections. Advanced Mastery is for those who are especially curious or wish to do more to understand the topics explained above. If you want, you can skip this part and move on to the next section.