fast tooth
fast tooth

Reputation: 2459

string escape characters

Can someone explain the following:

In [9]: str( """w'o"w""") 
Out[9]: 'w\'o"w'

why the double quote has no escape? it does the same thing with or without the escape:

In [10]: print ( 'w\'o"w') 
w'o"w

In [11]: print ( 'w\'o\"w') 
w'o"w

And in the following two cases there's no escape:

In [12]: str( """w'o'w""")
Out[12]: "w'o'w"

In [13]: str( """w"o"w""")
Out[13]: 'w"o"w'

but they are still equivalent to their with escape versions:

In [14]: str("w\'o\'w")
Out[14]: "w'o'w"

In [15]: str( "w\"o\"w") 
Out[15]: 'w"o"w'

Can someone explain to me why Python has this 'inconsistency'? What are the merits of having them?

Upvotes: 1

Views: 382

Answers (3)

rmunn
rmunn

Reputation: 36688

When the Python console (which includes IPython) shows the value of a string, it does so by printing the repr() of the string. repr() will produce output that could be parsed by the Python parser, so it will include backslash-escapes when necessary. (And only when necessary). If all the quotes in a string are single-quotes, repr() will show you the string with double-quotes around it, necessitating no backslashes. If all the quotes in a string are double-quotes, repr() will show you the string with single-quotes around it, necessitating no backslashes. If the string contains mixed single- and double-quotes, then backslashes will be needed.

As for what's going on with the print() calls -- they are not printing the repr() of the string, they're showing the str() value of that string, which is the string itself with no backslash escaping added (and no quotes around it).

So here's what's going on in each of your examples:

In [9]: str( """w'o"w""") 
Out[9]: 'w\'o"w'

You are getting the value of the string w'o"w and IPython is displaying the value on the output. So it calls repr() on the value, which puts quotes around it and backslashes inside it.

In [10]: print ( 'w\'o"w') 
w'o"w

You are printing the string w'o"w, not getting its value. So IPython is displaying the printed string. Note there's no Out[10] -- the print() function returns None, and IPython suppresses output values when they are None.

In [11]: print ( 'w\'o\"w') 
w'o"w

Same thing here: you're printing the string, rather than IPython showing you the value. So it uses str(), not repr().

In [12]: str( """w'o'w""")
Out[12]: "w'o'w"

Here you are showing the value of the string, so it gets quotes around it. But no backslashes, since they're not needed to represent the string.

In [13]: str( """w"o"w""")
Out[13]: 'w"o"w'

Ditto here: no backslashes, since they're not needed to represent the string.

In [14]: str("w\'o\'w")
Out[14]: "w'o'w"

Ditto here: no backslashes in the repr() output, since they're not needed to represent the string.

In [15]: str( "w\"o\"w") 
Out[15]: 'w"o"w'

And ditto here as well: no backslashes in the repr() output, since they're not needed to represent the string.

I hope this helps you understand what's going on a bit better.

Upvotes: 4

aruisdante
aruisdante

Reputation: 9085

The only reasonn you have to escape the start/end of string character is so the compiler/interpreter knows that that symbol doesn't mean the start/end of the string scope.

Therefore, If the character is ', then ' becomes a special character, and " is not. And vise versa. And if you start it with ''' or """ (multi line string) than neither character is special and thus neither needs escaping.

Upvotes: 0

Martin Konecny
Martin Konecny

Reputation: 59611

When you enclose a string with single quotes, you need escape the single quotes within the string - otherwise how would the interpreter know if the string is meant to be ended or not?

Conversely, when you are enclosing a string with double quotes, you need to escape the double quotes within the string.

""" enclosing quotes is an exception - it is smart enough to change it's behaviour based on the contents of the string being enclosed. If you enclose a String containing a single quote, it will behave as if you had enclosed it with double quotes. If you enclose a String containing a double quote, if will behave as if you had enclosed it with single-quotes

Upvotes: 0

Related Questions