Have you thought about what might happen if we wanted to display a quotation mark on the screen? Since everything we want to display is contained between quotation marks in the print()
statement, putting a quote inside the quotes would be a problem.
Most programming languages allow for "escape sequences", where you signal with some sort of escape character that the next character you see shouldn't be handled in the normal way.
The following code demonstrates many of Python's escape sequences.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Learn Java the Hard Way Learn Java the "Hard" Way! Hello Jelly \ # -=- \ // \\ \\\ \\\\ +---+ |o | | o | | o| +---+ +---+ |o | | o | | o| +---+ I hope you understand "escape sequences" now. I hope you understand "escape sequences" now.
We will import Python 3's print function as the first line of our program. We'll pretty much always have this as the first line, even though it might not always be necessary.
Like most programming languages, Python's escape character is a backslash ("\
"), which is the same key you press to make a pipe ("|
") show up but without holding Shift
. All escape sequences in Python must be somewhere inside a set of quotes.
\"
represents a quotation mark.
\t
is a tab; it is the same as if you pressed the Tab
key while typing your code. In most terminals, a tab will move the cursor enough to get to the next multiple of 8 columns.
It probably seems more complicated now because you've never seen it before, but when you're reading someone else's code a \t
inside the quotes is less ambiguous than a bunch of blank spaces that might be spaces or might be a tab. Personally, I never ever press the Tab
key inside quotation marks.
\n
is a newline. When printing it will cause the output to move down to the beginning of the next line before continuing printing.
\\
is how you display a backslash.
You will notice that the first line begins with a pound sign (or "hash sign", or "octothorpe"). This marks the line as a "comment", which is in the program for the human programmers' benefit. Comments are totally ignored by the computer.
In fact, as shown on lines 7 and 8, the symbol that marks a comment doesn't have to be at the beginning of the line.
Some programming languages have a way to mark a "block comment" that lasts over several lines until you close it. Python does not, but lots of people use a triple-quoted string for that purpose, as shown on lines 10 and 11.
Lines 13 and 14 demonstrate that to get a backslash to show up on the screen you must escape the backslash with another backslash.
There's something neat on line 17. If you use triple-quotes (three quotation marks in a row), then you can start writing your string and just keep going through multiple lines until you use three more quotes to end. Three single quotes also work.
And finally on line 26 you'll see that you can sometimes avoid using an escape sequence if you are clever about which type of quote you use.
- Reopen the code for the previous exercise (
printingchoices.py
). Save a copy asprintingchoicesescapes.py
. Rewrite it so that the output looks the same as the originalprintingchoices.py
but that it only uses a singleprint()
statement and a bunch of escape sequences.
©2017 Graham Mitchell