AlgoCademy
Lesson
Code

Escaping characters in {lang}


In {lang}, some characters serve different functions and so cannot be displayed or printed like other characters. These characters are referred to as special characters.

For example, double quotes (") and single quotes (') are considered special characters by {lang}. Their job is to let {lang} know when a string declaration starts / ends.

That's why in the previous lesson we ran into problems when trying to have a single quote inside a string that is surrounded by single quotes:

print('My name's Andy')

We've found an elegant solution to this and that was by changing the type of quotes we surround our string in:

print("My name's Andy")

print('Double quotes "inside double" quotes')

But what if we wanted to print a string that contains both single and double quotes such as After "Coding 101" we'll solve some problems ?

Let's try to surround this text by single quotes:

print('After "Coding 101" we'll solve some problems')

That doesn't work. {lang} interprets After "Coding 101" we to be our string.

Maybe surrounding the text by double quotes will work:

print("After "Coding 101" we'll solve some problems")

Not really. Now {lang} interprets After to be our string.

Character escaping comes to our rescue!


Escaping characters:

There is a way to print special characters like these in {lang}, and that is by using the backslash character (\).

When we put \ before a special character, we tell {lang}:

"Hey, I know this is a special character, but I don't want to use it like that here. I just want to print it like I print any other character."

For example, when putting a \ before a quote, {lang} no longer interprets that as being the end of the string:

print('My name\'s Andy')

print("Double quotes \"inside double\" quotes")

print("After \"Coding 101\" we'll solve some problems")

print('After "Coding 101" we\'ll solve some problems')

The output of this code is:

My name's Andy
Double quotes "inside double" quotes
After "Coding 101" we'll solve some problems
After "Coding 101" we'll solve some problems

Escaping backslash:

Sometimes we might want to print strings that contain character \, such as a file path in your PC:

print('C:\Users\Andy\Games')

This code looks perfectly fine at first sight, but if you run it, it would produce a SyntaxError.

When the interpreter reaches \U, it supposes that you are trying to escape character U, which is not a special character thus cannot be escaped.

Now that you've learned about its function, what type of character do you think \ is in {lang}? It's a special character!

And like any special character, it can also be escaped by putting another \ in front of it (\\):

print('C:\\Users\\Andy\\Games')

The output of this code is:

C:\Users\Andy\Games

New line character:

Similarly, the escape sequence can be used to make an otherwise printable character serve a special function.

For example, the letter n can be printed normally, but adding a backslash before it (\n) will indicate the start of a new line:

print("Hello world!\nWhat a good day for coding!\n")
print("Let\'s have a blast!")

The output of this code is:

Hello world!
What a good day for coding!

Let's have a blast!

Notice the empty line after What a good day for coding! ?

That's one new line from the \n plus the new line that print() prints no matter what the message is.


Assignment
Follow the Coding Tutorial and let's escape some characters!


Hint
Look at the examples above if you get stuck.