- [Instructor] Next, we're going to take a look
at what are known as raw strings in Python.
Now you may recall that the backslash character
within a string normally introduces an escape sequence.
So for example, Backslash + n if it were to appear
within a string would represent the
new line escape sequence.
Now, when you're going things like creating file paths
on the Microsoft Windows operating system,
such as the example I just pasted in here,
backslashes are used to separate your folder names.
So, if you're creating a string
that represents a Windows path you would need
to use two backslashes in every location
where a backslash is required in a Windows path
because by default the backslash inside of a string is used
to indicate an escape sequence.
So, you'd actually have
to use the backslash characters escape sequence
to properly encode that string
and if I go ahead and display
that you can see we get the same thing
as we had entered in the string up above here.
Now, that can be really painful to do
when you're dealing with Windows paths a lot,
or as we go into the next sequence of videos
and start to introduce regular expressions.
Backslashes are used in regular expressions
for pattern matching quite a lot.
So, there's a concept in Python called a raw string
and with a raw string it just assumes every character
in the string is its literal value.
So, this is the same file path as a raw string,
which is a string starting with the letter r
before its opening quote and as you can see here I'm
not using double backslashes throughout this Windows path
that I've created.
However, when I evaluate that same path you can see
that it is still creating the string properly
with the two backslashes
at each location underneath the hood.
So, this is a convenience notation
for anytime you're dealing with strings
that can contain literal backslash characters in particular
and other escape character sequences as well
that you simply want to be treated
as their individual character values.