In context of Python Raw string
Asked Answered
N

1

9

My Python version is:

~$ python --version  
Python 2.6.6

I tried following in Python (I wants to show all):

1: \ use as escape sequence

>>> str('Let\'s Python')       
"Let's Python"

2: \ use as escape sequence

>>> 'Let\'s Python'             
"Let's Python"

3: str() and print as value not type

>>> print 'Let\'s Python'       
Let's Python

4: its Python a raw string

>>> repr('Let\'s Python')      
'"Let\'s Python"'

[QUESTION]

5: Python raw string

>>> print r'Let\'s Python'    
Let\'s Python

6: This, I do not understand followings:

>>> r'Let\'s Python'            
"Let\\'s Python"

>>> r'\\'
'\\\\'

Why \\ ? Why output in 5 and 6 are different?
r and repr() are same not same?
Also please explain about internal representation of string and raw strings are same or different.

Notify answered 2/12, 2012 at 13:24 Comment(0)
C
14

You are confusing raw string literals r'' with string representations. There is a big difference. repr() and r'' are not the same thing.

r'' raw string literals produce a string just like a normal string literal does, with the exception to how it handles escape codes. The produced result is still a python string. You can produce the same strings using either a raw string literal or a normal string literal:

>>> r'String with \n escape ignored'
'String with \\n escape ignored'
>>> 'String with \\n escape ignored'
'String with \\n escape ignored'

When not using a r'' raw string literal I had to double the slash to escape it, otherwise it would be interpreted as the newline character. I didn't have to escape it when using the r'' syntax because it does not interpret escape codes such as \n at all. The output, the resulting python string value, is exactly the same:

>>> r'String with \n escape ignored' == 'String with \\n escape ignored'
True

The interpreter is using repr() to echo those values back to us; the representation of the python value is produced:

>>> print 'String'
String
>>> print repr('String')
'String'
>>> 'String'
'String'
>>> repr('String')
"'String'"

Notice how the repr() result includes the quotes. When we echo just the repr() of a string, the result is itself a string, so it has two sets of quotes. The other " quotes mark the start and end of the result of repr(), and contained within that is the string representation of the python string String.

So r'' is a syntax to produce python strings, repr() is a method to produce strings that represent a python value. repr() also works on other python values:

>>> print repr(1)
1
>>> repr(1)
'1'

The 1 integer is represented as a string '1' (the character 1 in a string).

Catchweight answered 2/12, 2012 at 13:39 Comment(6)
Thanks Martijn!...Although I still not understood why >>> print r 'String with \n escape ignored' prints String with \n escape ignored ...single `` ,Notify
@GrijeshChauhan: Now try that without the r (don't change anything else). The \n becomes a newline.Catchweight
Another thing to show the difference: len(r'\n') versus len('\n'). The first is two characters, a \ backslash and an n, the other is one character, a newline.Catchweight
Thanks @MartijnPieters. Is there any raw_repr(r"too many \\\ ") function that prints the argument back exactly the same, including the r' prefix? The output or repr()` is unreadable. len() and binascii workarounds are not fun at all.Hawkins
@Hawkins no, there isn’t. There is a single canonical representation.Catchweight
I found this in the mean time: #13779071. Spoiler alert: it's no silver bullet.Hawkins

© 2022 - 2024 — McMap. All rights reserved.