Escaping the semicolon?
Tim Chase
python.list at tim.thechases.com
Tue Dec 4 11:07:33 EST 2007
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')
> '123\\;abc'
You're asking the interpreter to print a representation of your
string, so it does so. Representations wrap the results in
quotes and escape characters within that need escaping.
>>> s.replace(';', '\;')
'123\\;abc'
>>> print repr(s.replace(';', '\;'))
'123\\;abc'
>>> print s.replace(';', '\;')
123\;abc
Additionally, it's best-practice to use raw strings or literal
backslashes, making your replacement either
r'\;'
or
'\\;'
because depending on the character following the back-slash, it
may be translated as a character you don't intend it to be.
-tkc
More information about the Python-list
mailing list