Let’s
try a program that will not work. Create a new Python file, type the following
exactly as shown, and run it:
When
the Python interpreter runs this code, it does not print the words "Hello
There". Instead, it prints an error and quits. Why?
Notice
the lack of quotation marks. Our previous examples all had quotation marks
around the words we wanted to print.
In
order to distinguish between code
(i.e. instructions) and text (regular
words, novels, magazine articles, et cetera), Python and almost all other
programming languages have a concept called strings.
DEFINITION: A string is text that is placed between
quotes to let Python know that it is not part of the code.
In
print instructions (also known as print statements), the word print is code, and the
characters you want to print are a string.
So we can fix the example above by adding quotes around the characters we want to print:
Notice
how the string (the characters between quotation marks) is highlighted a
different color than the word print. Most code editors
will highlight strings a different color than code.
The
requirement to put strings inside quotation marks is a rule of the Python
language (just like the requirement to end sentences with periods is a rule of
the English language). These rules are also known as syntax.
SYNTAX: To print a string in Python, use the word print, followed by a string to be printed:
print "This is correct syntax."