raw string

In Python, a raw string is a string that you create by prefixing its literal with an r or R and that treats backslashes (\) as literal characters without their usual special meaning in escape sequences, such as \n for a newline and \t for a tab.

Raw strings are particularly useful when dealing with regular expressions, file paths, or any other scenario where backslashes are common and could be interpreted as escape sequences.

Example

Here’s an example that uncovers the difference between normal and raw strings:

Python
>>> # Normal string with escape sequences
>>> print("This is a new line:\nAnd this is a tab:\tEnd of line.")
This is a new line:
And this is a tab:  End of line.

>>> # Raw string with escape sequences
>>> print(r"This is a new line:\nAnd this is a tab:\tEnd of line.")
This is a new line:\nAnd this is a tab:\tEnd of line.

In the normal string, the escape sequences \n and \t are interpreted as a newline and a tab, respectively. In the raw string, these sequences are printed as they are, with the backslashes included.

Tutorial

What Are Python Raw Strings?

In this tutorial, you'll learn the nuances of using raw string literals in your Python source code. Raw strings offer convenient syntax for including backslash characters in string literals without the complexity of escape sequences.

basics python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 25, 2025