How To Use Python Raw String

Introduction

You can create a raw string in Python by prefixing a string literal with r or R. Python raw string treats the backslash character (\) as a literal character. Raw string is useful when a string needs to contain a backslash, such as for a regular expression or Windows directory path, and you don’t want it to be treated as an escape character. This article covers the basics of how Python raw strings work and provides a few common examples of how to use raw strings to include special characters in strings.

The examples in this article use the Python interactive console in the command line to demonstrate different raw string scenarios.

Including a Newline Character in a String Using Raw String

This example uses a string with a value: Hi\nHello. If you try to assign this value to a normal string, then the newline character (\n) creates a new line:

  1. s = 'Hi\nHello'

Print the string:

  1. print(s)

The output is:

Hi
Hello

The output shows that the newline character results in a new line.

To include the newline character in the string, prefix the string variable with r or R to create a raw string:

  1. raw_s = r'Hi\nHello'

Print the string:

  1. print(raw_s)

The output is:

Hi\nHello

The output includes the newline character.

Including Double Backslash Characters in a String Using Raw String

If you try to include double backslash characters, such as for a hostname path, in a normal string, then the first backslash character won’t print because the compiler considers the backslash to be an escape indicator.

For example, create a string that contains a path:

  1. s = '\\examplehost\digitalocean\content\'

Print the string:

  1. print(s)

The output is:

\examplehost\digitalocean\content\

The output shows that the first backslash character isn’t included in the string.

To include both backslash characters in the string, prefix the string variable with r or R to create a raw string:

  1. s = r'\\examplehost\digitalocean\content\'

Print the string:

  1. print(s)

The output is:

\\examplehost\digitalocean\content\

The output includes both backslash characters.

Troubleshooting Quotes and Backslash Characters in Raw Strings

In a raw string, quotes can still be escaped with a single backslash character, however, the backslash character remains in the resulting raw string.

In addition, a raw string can’t end with an odd number of backslash characters. Because of this feature, you can’t create a raw string that contains a single backslash character, so r"/" is an invalid string.

Invalid Raw String Examples

In this example, the end quote is missing from the output since it’s being escaped by the backslash character resulting in an unterminated string literal error:

r'\'

In this example, the first two backslashes will escape each other, and the third one will try to escape the end quote, resulting in an unterminated string literal error:

r'ab\\\'

Valid Raw String Examples

Here are some examples of valid raw strings that include quotes and backslash characters.

Create a raw string that escapes quotes:

  1. s = r"\"\""

Print the string:

  1. print(s)

The output is:

\"\"

The output shows that the backslash characters escape the quotes so that the string doesn’t terminate, but the backslash characters remain in the result string.

Create a raw string with an even number of backslash characters:

  1. s = R'ab\\'

Print the string:

  1. print(s)

The output is:

ab\\

The output shows that the even number of backslash characters are included in the result string.

Conclusion

In this article, you learned the basics of raw strings in Python. Continue your learning about Python strings.

Source:
https://www.digitalocean.com/community/tutorials/python-raw-string