Last Updated on July 22, 2022 by Jay
This tutorial will explain what’s a Python raw string or r-string and how to use it in practice. We’ll go through a few examples.
A Common Problem With File Path
Usually, when we need to read a file on our computer, we’ll need to specify the file location, which might be something like this: C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx
However, if we type that file path directly into Python as a string, we’ll get a SyntaxError message that ‘unicodeescape’ codec can’t decode bytes.
The way to quickly fix this problem is using a Python raw or r-string, which usually starts with an r before the actual string part “…”. Keep reading to learn what an r-string is and alternative workaround methods.
'C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx' SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
This error message is due to the use of backslash \, which is a special character in Python, also called the “escape character.” For example, a newline character is \n. For example:
print('this is the first line\nthis is the second line') this is the first line this is the second line
We should remember that a backslash has special meanings, and we shouldn’t use them unless for a special character in string data.
Working Around Escape Characters
Let’s take a look at several ways of working around those escape characters.
Change Backslash To Forward-slash
One way to avoid the SyntaxError is by changing all the backslash \ to forward-slash /, which isn’t a special character. For instance:
Remember that a single backslash followed by a certain character is an “escape character.” Double backslash \\ means a literal backslash in a string! Knowing this, we just need to add an additional backslash next to all the existing backslashes!
Python Raw String R-string
The above two methods work, but both require extra effort by either changing or adding several characters to the string. A Python r-string allows us to fix the problem very quickly. Instead of modifying the original string, we need to add a character r (or capital letter R) right in front of the string part.
The effect of adding r (or R) is to instruct Python to treat whatever follows it as a literal string and ignore the backslash as a special character. So a backslash \ literally means a backslash \ if used with an r-string!
print(r'C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx') C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx print(R'C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx') C:\Users\jay\Desktop\PythonInOffice\raw_string\raw_string_eg.xlsx