Python Raw Strings Explained: How to Use r'' Strings Correctly
Learn how Python raw strings work, when to use r'' strings, and how to handle backslashes, newlines, regex, and common raw string pitfalls.
Drake Nguyen
Founder · System Architect
Introduction
In Python, a raw string is created by prefixing a string literal with r or R. A raw string treats the backslash character (\) as a literal character instead of interpreting it as an escape sequence.
Raw strings are especially useful when working with regular expressions, Windows file paths, or any string that contains many backslashes. They help keep strings readable and prevent unintended escape behavior.
This article explains how Python raw strings work, when to use them, and common mistakes to avoid.
Including a Newline Character Using Raw Strings
In a normal Python string, the sequence \n represents a newline. Consider the following example:
s = "Hi\nHello"
print(s)
The output is:
Hi
Hello
The newline character creates a line break. If you want to keep \n as literal text, you can use a raw string:
raw_s = r"Hi\nHello"
print(raw_s)
The output is:
Hi\nHello
This behavior is useful when working with patterns where escape sequences should not be interpreted.
Using Raw Strings with Backslashes
Backslashes are commonly used in file paths and network locations. In a normal string, backslashes must be escaped.
path = "\\\\examplehost\\netalith\\content\\"
print(path)
This syntax quickly becomes hard to read. A raw string simplifies the same path:
path = r"\\examplehost\netalith\content\"
print(path)
The output is:
\\examplehost\netalith\content\
Raw strings make file paths and regex patterns significantly easier to understand and maintain.
Limitations of Python Raw Strings
Although raw strings are powerful, they have a few important limitations.
- A raw string cannot end with an odd number of backslashes
- Quotes can still be escaped, but the backslash remains in the string
- Raw strings do not disable all escaping behavior
Invalid Raw String Examples
The following examples are invalid and will raise syntax errors:
r"\"
r"abc\"
In these cases, the final backslash escapes the closing quote, resulting in an unterminated string literal.
Valid Raw String Examples
Raw strings work correctly when backslashes appear in even numbers or are not placed at the end.
s = r"ab\\"
print(s)
The output is:
ab\\
Raw strings can also contain escaped quotes:
s = r"\"\""
print(s)
The output is:
\"\"
The backslashes prevent the quotes from ending the string, and they remain part of the final value.
When Should You Use Raw Strings?
Raw strings are ideal in the following scenarios:
- Regular expressions (
r"\d+\.\d+") - Windows file paths (
r"C:\Users\Admin") - String patterns with many backslashes
- Improving readability of escape-heavy strings
Conclusion
Python raw strings provide a clean and reliable way to work with backslashes and escape-heavy text. By understanding their behavior and limitations, you can write clearer, safer, and more maintainable code.
As you continue working with Python strings, raw strings will become an essential tool—especially when dealing with regular expressions and file paths.