[Tutor] FW: wierd replace problem
wprins at gmail.com
Tue Sep 14 12:41:28 CEST 2010
On 14 September 2010 11:09, James Mills <prologic at shortcircuit.net.au>wrote:
> $ python
> Python 2.6.5 (r265:79063, Jun 13 2010, 14:03:16)
> [GCC 4.4.4 (CRUX)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> s = "foo\"bar'"
> >>> s
I'd like to point something out here. Typing "s" as James showed here at
the prompt outputs a version of the string that Python will understand if
fed again, consequently it's encoded to do the same escaping of characters
as you would have to do if you put that expression into your python source
yourself. If however you entered:
... then Python would've print the value of s as it really is, withou any
escape characters or anything else, e.g.:
>>> print s
So, even though you see a \' in the output posted by James above, the \ that
was output by Python isn't actually part of the string (e.g. it's not really
there as such), it was only output that way by Python to disambiguate the
value of the string.
So, at the risk of belaboring this point to death, if you do:
s = '\'\'\''
then the contents of the string s in memory is '''
The string does not contain the slashes. The slashes are only there to help
you make Python understand that the quotes must not be interpreted as the
end of the string, but rather as part of the string. You could get the
exact same result by doing:
s = "'''"
Here there's no ambiguity and consequently no need for slashes since the
string is delineated by double quotes and not single quotes.
Hope that helps.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor