Escaping the semicolon?
Mel
mwilson at the-wire.com
Tue Dec 4 11:11:02 EST 2007
Nick wrote:
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')
> '123\\;abc'
>
> I just wanted a single backslash. I can see why this probably happens
> but i wondered if it is definitely intentional.
What you're seeing on the screen is a "literalization" of the string
value for the sake of the display. Consider
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '123;abc'
>>> b = s.replace(';', '\;')
>>> b
'123\\;abc'
>>> len(b)
8
The length suggests that there's only one backslash in the string.
Mel.
On the other hand
>>> repr(b)
"'123\\\\;abc'"
Isn't what I expected. No, wait, it is. It's the value of repr(b)
repred by the Python display logic.
MPW
More information about the Python-list
mailing list