What does \r do in python string?

I want to understand why do we use a r before a path name in python such as

dirname = r'C:\temp\parts'

recursive

81.9k32 gold badges147 silver badges236 bronze badges

asked Nov 16, 2015 at 5:29

0

r means the string will be treated as raw string.

See the official Python 2 Reference about "String literals":

When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. For example, the string literal r"\n" consists of two characters: a backslash and a lowercase 'n'. String quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character). Note also that a single backslash followed by a newline is interpreted as those two characters as part of the string, not as a line continuation.

Wevah

28.1k7 gold badges85 silver badges72 bronze badges

answered Nov 16, 2015 at 5:30

What does r do in python string?

Rahul TripathiRahul Tripathi

163k31 gold badges267 silver badges322 bronze badges

2

R Means ‘Raw String’

An ‘r’ before a string tells the Python interpreter to treat backslashes as a literal (raw) character. Normally, Python uses backslashes as escape characters. Prefacing the string definition with ‘r’ is a useful way to define a string where you need the backslash to be an actual backslash and not part of an escape code that means something else in the string.

Examples

1. In this example, Python will interpret each ‘\t’ found in this string as a tab. That is, the backslash+t is interpreted as an escape sequence with a special purpose.

>>> 'path\to\the\thing'
'path\to\the\thing'
>>> print('path\to\the\thing')
path o he hing
>>>

2. By adding the leading r, Python will know that the backslashes are to be interpreted as literal characters and not escape sequences. Interestingly, note how Python represents the literal backslash–as an escape sequence of backslash + backslash.

>>> r'path\to\the\thing'
'path\\to\\the\\thing'
>>> print(r'path\to\the\thing')
path\to\the\thing
>>>

3. This means another way to handle the literal backslash problem is to use backslash + backslash in your string definition. However, this feels like a clunkier way to define the string to me when compared to using ‘r’. Using ‘r’ makes for, I think, more readable code.

>>> 'path\\to\\the\\thing'
'path\\to\\the\\thing'
>>> print('path\\to\\the\\thing')
path\to\the\thing
>>>

For More Information

Python 3 Reference, section 2.4.1, “String and Bytes literals”

MORE FREE STUFF!

Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.

Python Raw String

Let’s say we want to create a string Hi\nHello in python. If we try to assign it to a normal string, the \n will be treated as a new line.

s = 'Hi\nHello'
print(s)

Output:

Hi
Hello

Let’s see how raw string helps us in treating backslash as a normal character.

raw_s = r'Hi\nHello'
print(raw_s)

Output: Hi\nHello Let’s see another example where the character followed by backslash doesn’t have any special meaning.

>>> s = 'Hi\xHello'
  File "", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \xXX escape

We got the error because python doesn’t know how to decode ‘\x’ as it doesn’t have any special meaning. Let’s see how we can create the same string using raw strings.

>>> s = r'Hi\xHello'
>>> print(s)
Hi\xHello

If you are on Python console and create a raw-string like below.

>>> r'Hi\xHello'
'Hi\\xHello'

Don’t get confused with the output having two backslashes. It’s just to show it as a normal python string where backslash is being escaped.

Python Raw String and Quotes

When a backslash is followed by a quote in a raw string, it’s escaped. However, the backslash also remains in the result. Because of this feature, we can’t create a raw string of single backslash. Also, a raw string can’t have an odd number of backslashes at the end. Some of the invalid raw strings are:

r'\'  # missing end quote because the end quote is being escaped
r'ab\\\'  # first two backslashes will escape each other, the third one will try to escape the end quote.

Let’s look at some of the valid raw string examples with quotes.

raw_s = r'\''
print(raw_s)

raw_s = r'ab\\'
print(raw_s)

raw_s = R'\\\"'  # prefix can be 'R' or 'r'
print(raw_s)

Output:

\'
ab\\
\\\"

That’s all for a quick introduction of python raw string.

You can checkout complete python script and more Python examples from our GitHub Repository.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

What does \r do in string?

r means the string will be treated as raw string. See the official Python 2 Reference about "String literals": When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string.

What does end \r do in Python?

Conceptually, \r moves the cursor to the beginning of the line and then keeps outputting characters as normal. You also need to tell print not to automatically put a newline character at the end of the string. In python3, you can use end="" as in this previous stackoverflow answer.

What does an r before a string mean in Python?

Introduction the Python raw strings In Python, when you prefix a string with the letter r or R such as r'...' and R'...' , that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters.

What does r before a string mean?

R Means 'Raw String' An 'r' before a string tells the Python interpreter to treat backslashes as a literal (raw) character. Normally, Python uses backslashes as escape characters.