How To Use Python Raw String?
You can define a Python raw string by placing an ‘r’ or ‘R’ before a string literal. A raw string in Python interprets the backslash (\) as a regular character, preventing it from acting as an escape character.
This is particularly handy when you want to include backslashes in strings, like in regular expressions or Windows file paths. This guide delves into the fundamentals of Python’s raw strings, offering examples to showcase their utility, especially when incorporating special characters within strings.
Including a Newline Character in a String Using Raw String
In this example, the string contains the value: Hi\nHello World.
When you attempt to assign this value to a standard string, the newline character (\n) results in a line break
Example
var_data = 'Hi\nHello World
#print string
print(var_data)
Output
Hi Hello World
The result indicates that the newline character causes a line break.
To add a new line in the text, start the text with r or R to make it a raw string.
Example
var_data = r'Hi\nHello World'
#print string
print(var_data)
Output
Hi\nHello World
The result shows the newline character.
Using Raw Strings to Add Double Backslash Characters within a String
If you attempt to use two backslashes in a regular string, like for a file path, the first backslash might not show up because the system thinks it’s trying to do something special with it.
For instance, make a string with a path:
Example
var_data = '\\Desktop\home\jenny'
#print string
print(var_data)
Output
\Desktop\home\jenny<
The output indicates that the string does not include the initial backslash character.
To ensure both backslash characters appear in the string, use the `r` or `R` prefix to define it as a raw string
Example
var_data = r'\\Desktop\home\jenny'
#print string
print(var_data)
Output
\\Desktop\home\jenny
The output contains both backslash characters.
Addressing Issues with Quotes and Backslashes in Raw Strings
You can use a single backslash to escape quotes in a raw string, but the backslash stays in the string. Also, a raw string must have an even number of backslashes at its end. You can’t make a raw string with just one backslash. For example, r”/” won’t work.
Invalid Raw String Examples
In this example, the missing end quote causes an error because the backslash tries to escape it, leading to a string error:
Example
r'\'
Output
ERROR! File “<string>”, line 1 Â Â r’\’ Â Â ^ SyntaxError: unterminated string literal (detected at line 1)
In this example,
The first two backslashes cancel each other out, but the third one aims to escape the closing quote, causing a string error
Example
r'ab\\\'
Output
ERROR! File “<string>”, line 1 Â Â r’ab\\\’ Â Â ^ SyntaxError: unterminated string literal (detected at line 1)
Valid Raw String Examples
Here are some correct raw strings that have quotes and backslashes. Make a raw string that uses backslashes to handle quotes:
Example
str_data = r"\"\""
print(str_data)
Output
\”\”
The result indicates that the backslashes prevent the quotes from ending the string, but they still appear in the final string. Make a raw string that has an even count of backslashes.
Example
str_data = R'Hello\\'
print(str_data)
Output
Hello\\
The result includes an even number of backslashes in the string.
Conclusion
You discovered the fundamental aspects of using raw string in Python in this guide. Discover Python functions with our articles.