Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In context of Python Raw string

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.

like image 996
Grijesh Chauhan Avatar asked Dec 02 '12 13:12

Grijesh Chauhan


People also ask

What is a raw string in Python?

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.

What is a raw string?

Raw String Literal in C++ Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \” ' of C++ are not processed. Hence, a raw string literal that starts with R”( and ends in )”.

What is raw string give an example?

What is raw string give an example? raw strings are raw string literals that treat backslash (\ ) as a literal character. For example, if we try to print a string with a “\n” inside, it will add one line break. But if we mark it as a raw string, it will simply print out the “\n” as a normal character.

How do you raw string a variable in Python?

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.


1 Answers

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).

like image 182
Martijn Pieters Avatar answered Nov 15 '22 21:11

Martijn Pieters