Escape Sequences
00:00 Strings: Escape Sequences. As you saw earlier on, it’s possible to need single or double quotes inside a string, and if you need to have both inside a string, it’s not simple to see how to do this.
00:18 Here’s an example where we have both double and single quotes inside a string, where we’re quoting something with an apostrophe inside it.
00:30 Because inside this string we have both double and single quotes, neither enclosing quote type is going to work. Trying to redefine it using single quotes won’t work either, although it will break down in a different place, as highlighted by bpython in color.
00:53 As you can see, it doesn’t work because here we’ve got that single quote as well as the double quotes inside. Initially, this may seem to be an insurmountable problem, but fortunately, a technique called escaping allows the normal interpretation of the character to be suspended and that string to be defined as normal.
01:15 This is done by putting the backslash character before either the single or double quotes within our string, allowing them to be stored safely inside the string without taking up their normal role. Here, the desired sentence is being defined using single quotes, and then when the apostrophe comes along, a backslash is placed before the apostrophe, stopping it being interpreted as the end of the string.
01:44
Now you can see that Python is happy with the definition of the variable because of the backslash used before the apostrophe. And looking at c
, you can see how it’s been stored internally. To see it the way it would be printed out for the user, we can use print()
, but you can see it’s slightly different in the Python REPL, as highlighted here. It’s enclosed in those single quotes, and also it doesn’t interpret the \'t
, whereas when it’s printed out that \'t
is reinterpreted as the single quote that we wanted. Using this technique, it is of course possible to have enclosed the string in double quotes and to have escaped these quotes inside, or even escape all of the quotes inside the text.
02:38 Now, there is another use of escape characters, which is for special meanings such as newlines, and you’re going to see that in action next.
02:51
Here, d
is being defined with the text "This is line 1."
followed by \n
, which together mean newline. This is then finished off with "This is line 2."
and the quotes are finished at the end.
03:08
So here, we have this \n
character which means newline. When it’s shown in the REPL, the \n
isn’t interpreted, but if we use print()
and print d
, you can see that This is line 1.
is on a separate line to This is line 2
. This is something you will use an awful lot if you’re working with text files, and often you’ll find you need to insert or remove newline characters from text.
03:36
It’s also possible to do it with tabs with \t
. So here you can see a variable is defined with some tab characters inserted in.
03:51 And once again, printing this out shows that the tabs have been inserted between the words, so those tabs have now been interpreted. And in this case, each tab is equal to eight characters, so you can see the spacing of the words is equal and it isn’t dependent on their length.
04:11
It’s important to be aware of the different escape characters you can insert into text, and the context in which they’re used will change their effect. But onscreen at the moment, you can see a selection of escape characters, such as backslash single quote (\'
) and double quote (\"
), newline with \n
, carriage return with \r
, tab, which you’ve already seen, and the ability to define characters using octal or hexadecimal values.
04:39 This isn’t an exhaustive list of escape characters, but it’s a good starting place.
Become a Member to join the conversation.