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 Show
recursive 81.9k32 gold badges147 silver badges236 bronze badges asked Nov 16, 2015 at 5:29
0
See the official Python 2 Reference about "String literals":
Wevah 28.1k7 gold badges85 silver badges72 bronze badges answered Nov 16, 2015 at 5:30
Rahul TripathiRahul Tripathi 163k31 gold badges267 silver badges322 bronze badges 2 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. 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. 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. 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. 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 StringLet’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.
Output:
Let’s see how raw string helps us in treating backslash as a normal character.
Output:
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.
If you are on Python console and create a raw-string like below.
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 QuotesWhen 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:
Let’s look at some of the valid raw string examples with quotes.
Output:
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.
|