How to Write a Raw String in Python
In a normal Python string, a backslash starts a special character: \n is a new line and \t is a tab. That breaks Windows paths such as C:\new\temp and regular expressions such as \d{4}. A raw string, written with an r in front, keeps every backslash as it is. This guide compares the two on Python 3.15.2.
Syntax
<name> = r"<text with backslashes>"
| Part | What it means |
|---|---|
r | Turns the string into a raw string. Upper case R works too |
"<text with backslashes>" | The text. Each \ stays a backslash instead of starting an escape sequence |
The Python language reference says raw string literals "treat backslashes as literal characters. As a result, in raw string literals, escape sequences are not treated specially".
The Example
A script writes the same Windows path twice, once as a normal string and once as a raw string, then prints both and their lengths.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
normal = "C:\new\temp"
raw = r"C:\new\temp"
print(normal)
print(raw)
print(len(normal), len(raw))

Step 2: Run It
uv run python raw.py
C:
ew emp
C:\new\temp
9 11

In the normal string, \n became a new line and \t a tab, so the path broke apart and lost two characters: 9 instead of 11. The raw string kept both backslashes. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A regular expression in a normal string: "\d{4}" | Python 3.15 warns: SyntaxWarning: "\d" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\d"? A raw string is also an option. | Write it as r"\d{4}" |
Ending a raw string with a backslash: r"C:\new\" | SyntaxError: unterminated string literal (detected at line 1); perhaps you escaped the end quote? | End it without the backslash, r"C:\new", and add the separator in code, or use pathlib to join paths |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A path prints over two lines or with a gap | \n or \t inside a normal string became a new line or a tab | Put an r in front of the string |
SyntaxWarning: "\d" is an invalid escape sequence | A backslash that isn't a known escape, in a normal string | Use a raw string, or double the backslash: "\\d" |
FAQ
Where do raw strings help most?
Wherever backslashes are part of the text: Windows paths and regular expressions. The reference's own example is a pattern, r'\d{4}-\d{2}-\d{2}'.
Can a raw string contain a quote?
Yes, but the backslash stays. The reference says: "Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result". It's often simpler to use the other kind of quote around the string: r'say "hi"'.
Conclusion
r"C:\new\temp" kept all 11 characters, while the same text without the r turned \n and \t into a new line and a tab. Use raw strings for Windows paths and regular expressions, and don't end one with a backslash.
Additional Resources
- Raw string literals — the reference, with its examples.
- Escape sequences — every sequence a normal string recognizes.
