How to Use a Raw Strings in Python?

In this tutorial, I will explain how to use a raw string in Python. As a data scientist working for a New York-based client, I had the task of processing text data stored in logs and ensuring the text strings were formatted correctly before analysis. The log files contain file paths, regex patterns, and other text with special characters like backslashes (\). To manipulate these strings efficiently, I often need to handle strings containing escape sequences, such as regular expressions or Windows-style file paths.

‘r’ Before a String in Python

Normally, Python interprets backslashes in a string as special escape characters. For example:

print("C:\new\test.txt") 

Output:

C:
ew      est.txt

I have executed the above example code and added the screenshot below.

Raw Strings in Python

\n is treated as a new line and \t as a tab. The ‘r’ before the string makes Python treat all backslashes as literal characters:

print(r"C:\new\test.txt")  # prints C:\new\test.txt

Output:

C:\new\test.txt

I have executed the above example code and added the screenshot below.

Use a Raw Strings in Python

An ‘r’ before a string tells the Python interpreter to treat backslashes as a literal (raw) character. This is called a raw string literal in Python.

Read How to Extract a Substring Between Two Characters in Python?

When to Use Raw Strings

Raw strings in Python are useful whenever you need backslashes in a string to be interpreted literally. Some common use cases:

  • Windows file paths like r"C:\Users\John\Documents"
  • Regular expressions that use \ for escaping
  • TeX or LaTeX which also use \ for commands

For example, suppose John from New York wants to search a text file for the pattern “fish\n”. Using a raw string avoids extra escaping:

import re
text = "I saw a fish\nin the water."
regex = r"fish\n"
if re.search(regex, text):
    print("Found fish!")

Output:

Found fish!

I have executed the above example code and added the screenshot below.

How to Use a Raw Strings in Python

Read How to Get the Last 4 Characters of a String in Python?

Caveats with Raw Strings

One thing to watch out for – you cannot end a raw string with a single backslash:

r"\" # raises an error 

If you need a trailing backslash, either use a normal string or add a second backslash:

r"\\"  # OK, produces a single backslash

Read How to Get the Last 3 Characters of a String in Python?

Conclusion

In this tutorial, I helped you to learn how to use a raw string in Python. I explained when to use raw strings and caveats with raw strings. This is convenient for strings containing Windows paths, regular expressions, LaTeX, or other content with lots of backslashes.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.