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.
\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.
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 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:
- 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?
Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn