In a raw string, escape sequence characters such as you saw in the last section are not interpreted. Now you’re going to see how to define a raw string and then how they are useful. Here, a new variable
a is defined with two pieces of text and a newline character in the middle.
You can see that when
a is printed out, it has that newline character obeyed when it’s printed. To create a raw string, the string is prefixed with an
r, as seen here. Otherwise, the content is exactly the same.
Printing this string out shows that the raw string means that the newline character is not interpreted by
print() and is left intact. This is useful in applications where the escape sequence characters are not to be interpreted normally, such as regular expressions, an area you may not be familiar with but one that’s extremely useful in text processing.
01:07 Regular expressions make use of the backslash character in a different way to normal text, and here raw strings come into their own by controlling the way that the string is interpreted. Regular expressions fall outside the scope of this course, but they’re well worth looking into if you ever need to process text.
Become a Member to join the conversation.
UweSteiner on June 16, 2020
good overview - thanks