Previous Lesson: Lesson 10. Functions with Parameters and Arguments
What are escape sequences in Python and what are they escaping from? Why do we need them anyway? You’ll find out once you’ve finished this lesson!
On the previous lesson, I was on Mac. But now, I’ll still be on my Mac. Anyhow, let’s create a new file: ex9.py. Then, type these in:
So what do you think will be the outcome of this code? Let’s see if you’re correct:
Nice. Before, we’ve learned about commenting. To do this, we use octothorpes and three pairs of single or double quotes. And that’s what we did on Line 1 and Line 2. But, as you may have noticed, we used three pairs of double-quotes to print a multi-line string on Line 10 to Line 15.
And then we also saw many \n between the months. You may be wondering about its purpose in life. But if we get back to our Terminal, we can see that it acted as a “newline”. And I’m gonna tell you that its function is for making newlines.
This is an example of an escape sequence. But Mr. Newline does not live alone. You can check out the other escape sequences on this reference.
These may be confusing. But once we’ve used them, we’ll come to realize what they are for. For instance, let’s try \’, \”, and \t. We’ll create a new file for this one so that our brains will not get mixed up. We’ll name the new file ex10.py. Once done, type these in. Remember to type these exactly:
Let’s run it:
Nice. What did these escapes do? Well, the \t was the easiest one here because its function is to have a TAB. But what do these other ones do?
Importance of Escape Sequences
We’ll see their importance once we’ve remove them from our lives because you see, you’ll never know how important something is until you’ve remove it from your life completely. What a sentimental line. Okay, let’s just remove the backslashes (\):
Woah! We can see that there were changes in colors. We’ll see if that kind of change can also change the result of our code in the Terminal:
An invalid syntax! Great! How come it became an invalid syntax? Well think of this: When we write a string with an opening single-quote, Python expects us to close it using a single-quote, too. Our opening single-quote was before the word You. And our closing single-quote was suppose to be after the do: But what had happened? It thought that we were closing it already after we have said You before the d. So Python thought we were saying this:
print(‘You’ Python was expecting to have a closing parentheses because we’ve already closed the string with a single-quote.
But, if we’re gonna do this inside a pair of double-quotes, then this line will work. So let’s change the opening and closing single-quotes to double-quotes:
And now let’s comment out Lines 2 down to Line 4. I’ll share with a little shortcut to do this. Just select these lines:
And now, hit the Command + /. Or, if you’re on Windows, Ctrl + /:
So if we run this, there should be no error here for us:
Nice. Alright, now that we know that there is no error on Line 1, let’s uncomment Line 2 by simply selecting that line and then pressing our little keyboard shortcuts:
When we run this:
Still good! Now let’s uncomment Line 3. You know how to do that:
Oops! That’s our problem! We can’t either change the single-quotes or the double-quotes into the other because this string uses both! So I guess there’s no escaping the escape sequences in Python. I hope you now know why this is an error.
We really need these escape sequences in Python
So now, let’s get our escape sequences back to their respective positions. Just hold on to the keys Command + Z or Ctrl + Z until you get back to this:
Good. Now let’s run this. This will be perfect. There will be no error for this one now:
Nice. I won’t explain everything here. You go on and experiment this on your own. Try bringing the backslashes back one by one. And then try removing them one by one. That’s where the learning, or should I say, your learning comes from–exploring things out on your own.
So in this lesson, we now understand why they’ve invented these escape sequences. And maybe the reason why they called these ‘escapes‘ is because these help us escape from error and unwanted results.
4 Replies to “Lesson 11. Escape Sequences in Python”