- Syntax
- Example 1: Creating a Raw String
- Example 2: Comparing Normal and Raw Strings
- Example 3: Windows File Path
- Example 4: Normal String for the Same Path
- Example 5: Tab Character
- Example 6: Regular Expression Pattern
- Example 7: Raw String with User Input Prompt
- Example 8: Using an Uppercase Prefix
- Example 9: Checking the Data Type
- Example 10: Mixing Forward Slashes and Backslashes
- Common Mistakes
- Best Practices
- Key Points to Remember
A raw string is a string in which Python treats the backslash (\) as a literal character instead of interpreting it as the beginning of an escape sequence.
A raw string is created by placing the letter r or R immediately before the opening quotation mark.
Raw strings are especially useful when working with Windows file paths, regular expressions, and strings containing many backslashes.
Without a raw string, Python interprets escape sequences such as \n, \t, and \\. With a raw string, these characters are preserved exactly as written.
Syntax #
r"string"
or
R"string"
Components #
| Component | Description |
|---|---|
r or R |
Prefix indicating a raw string. |
" " or ' ' |
Quotes enclosing the string. |
string |
Text stored exactly as written. |
Example 1: Creating a Raw String #
main.py
text = r"Hello\nPython"
print(text)
Output #
Hello\nPython
Explanation #
- The
rprefix creates a raw string. - Python does not interpret
\nas a newline. - The characters
\andnare printed exactly as written.
Example 2: Comparing Normal and Raw Strings #
main.py
print("Hello\nPython")
print(r"Hello\nPython")
Output #
Hello
Python
Hello\nPython
Explanation #
- The first string is a normal string.
\ncreates a new line.- The second string is a raw string.
\nis treated as ordinary text.
Example 3: Windows File Path #
main.py
path = r"C:\Users\Alice\Documents"
print(path)
Output #
C:\Users\Alice\Documents
Explanation #
- Windows paths contain many backslashes.
- Using a raw string avoids writing double backslashes.
- The path is stored exactly as written.
Example 4: Normal String for the Same Path #
main.py
path = "C:\\Users\\Alice\\Documents"
print(path)
Output #
C:\Users\Alice\Documents
Explanation #
- In a normal string, each backslash must be escaped using
\\. - The displayed output is the same as in the previous example.
- Raw strings are often easier to read.
Example 5: Tab Character #
main.py
print(r"Name\tAge")
Output #
Name\tAge
Explanation #
\tis not interpreted as a tab.- The characters
\andtappear in the output. - This demonstrates that escape sequences are ignored in raw strings.
Example 6: Regular Expression Pattern #
main.py
pattern = r"\d+"
print(pattern)
Output #
\d+
Explanation #
- Regular expressions frequently use backslashes.
- Raw strings make regex patterns easier to write.
- The pattern is stored exactly as written.
Example 7: Raw String with User Input Prompt #
main.py
print(r"Enter the path as C:\Folder\File.txt")
Output #
Enter the path as C:\Folder\File.txt
Explanation #
- The instruction contains several backslashes.
- Using a raw string keeps the path readable.
- No escape sequences are processed.
Example 8: Using an Uppercase Prefix #
main.py
text = R"Python\nProgramming"
print(text)
Output #
Python\nProgramming
Explanation #
- Both
randRcreate raw strings. - They behave identically.
- The choice between them is a matter of style.
Example 9: Checking the Data Type #
main.py
text = r"Hello\nWorld"
print(type(text))
Output #
<class 'str'>
Explanation #
- A raw string is still a normal Python string.
- The
rprefix only changes how the string literal is interpreted. - The data type remains
str.
Example 10: Mixing Forward Slashes and Backslashes #
main.py
path1 = r"C:\Users\Alice"
path2 = "C:/Users/Alice"
print(path1)
print(path2)
Output #
C:\Users\Alice
C:/Users/Alice
Explanation #
- Both paths are valid strings.
- Python also accepts forward slashes in many file path situations.
- Raw strings are mainly used when backslashes are preferred.
Common Mistakes #
1. Forgetting the r Prefix #
Incorrect
path = "C:\new\test"
print(path)
Output #
C:
ew est
Reason
Python interprets \n as a newline and \t as a tab.
Correct
path = r"C:\new\test"
print(path)
2. Assuming Raw Strings Are a Different Data Type #
Incorrect
text = r"Hello"
print(type(text))
Incorrect Expectation #
<class 'raw_string'>
Actual Output #
<class 'str'>
Reason
A raw string is still a string of type str.
3. Expecting Escape Characters to Work #
Incorrect
print(r"Hello\nWorld")
Incorrect Expectation #
Hello
World
Actual Output #
Hello\nWorld
Reason
Escape sequences are not interpreted inside raw strings.
4. Ending a Raw String with a Single Backslash #
Incorrect
path = r"C:\Users\Alice\"
Output #
SyntaxError: unterminated string literal
Reason
A raw string cannot end with a single backslash because it escapes the closing quotation mark.
Correct
path = r"C:\Users\Alice"
# or
path = "C:\\Users\\Alice\\"
Best Practices #
- Use raw strings for Windows file paths and regular expressions.
- Use normal strings when escape characters such as
\nor\tare required. - Remember that raw strings are still of type
str. - Use the lowercase
rprefix for consistency unless your coding standard specifies otherwise. - Avoid ending a raw string with a single backslash.
Key Points to Remember #
- A raw string is created using the
rorRprefix. - In a raw string, backslashes are treated as ordinary characters.
- Escape sequences such as
\nand\tare not interpreted. - Raw strings are useful for Windows file paths and regular expressions.
- A raw string is still of type
str. - A raw string cannot end with a single backslash.
- Choose between raw strings and normal strings based on whether escape processing is needed.