Python for Programmers: Raw Strings
Welcome to the Raw Strings lesson!
This lesson is shown as static text below. However, it's designed to be used interactively. Click the button below to start!
As in most languages,
\is an "escape" character inside of strings. For example, the string"\""is equivalent to'"'. The\"is an "escaped" double quote character, since a normal double quote would terminate the string.>
"\""Result:
'"'
Sometimes we want to write strings without any escaping at all. Python has "raw strings" for that purpose, written with an
rmodifier liker"hello". Inside of a raw string,\is just another character, with no special meaning.>
len('\"')Result:
1
>
len(r'\"')Result:
2
>
r'\"' == "\""Result:
False
>
r'\"' == '\\"'Result:
True
>
r'\"' == "\\\""Result:
True
In that last example, the raw string makes it easy for us to create a string containing a
\character followed by a"character. The regular double-quoted string makes it much more awkward: we have to escape the\character, then escape the"character.We can make
"\\\""better by converting it into a single-quoted string. But even then, we have to escape the\character.>
r'\"' == '\\"'Result:
True
Raw strings have two main uses. First, they can make a string easier to read, though this is very subjective in most cases. Most Python programmers will write
'"'instead of"\"". Some Python programmers will writer"\"instead of"\\". The more\characters we have, the more appealing raw strings become.Second, raw strings can help when we embed another language inside of a Python string. The most common example is regular expressions, where
\has a special meaning. It's much better to write a regular expression asr"\w\s\w"than as"\\w\\s\\w".>
r"\w\s\w" == "\\w\\s\\w"Result:
True
Finally, it's important to note that raw strings aren't a special data type. They give us regular Python strings.
>
r'\"' == '\\"'Result:
True
Both strings above contain exactly two characters: a backslash followed by a double quote. The only difference is in how we write that down in code. When the code runs, we get exactly the same value either way.