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: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.
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.
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: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.
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.
Become a Member to join the conversation.