What is string escaping and why should I care?

But wait a minute, you say, how about if you, for example, want to put a single quote character inside a single quote enclosed string?

Then you have to put a \ in front of the quote character. Like this:

'it is now five o\'clock'

This is called escaping, or an escape sequence.

Useful escape sequences include:

  • \' - for single quote

  • \" - for double quote

  • \n - for new line

  • \t - for tab

Examples of use of escape sequences:

'line 1\nline 2'
"Some \"good\" advice"

Let's try it out


Type the following into the the box where it says Enter source code here:

print('It is code o\'clock.');

Click Check source code.


Click Run!.

So what is happening here?

The function print() is taking one argument - a string containing a text to be printed.

When called, print() will print the given text given as the argument.

The escape sequence \' in the string is converted into '.

Now try this:

print('line 1\nline 2');
Next lesson:
What are comments?