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.txtI have executed the above example code and added the screenshot below.

\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.txtOutput:
C:\new\test.txtI have executed the above example code and added the screenshot below.

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.

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 backslashRead 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:
- How to Swap Characters in a String Using Python?
- How to Check if a String Contains All Unique Characters in Python?
- How to Check if a String Contains Only Alphanumeric Characters and Underscores in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.