string.replace

Jeff Raven jraven at psu.edu
Fri May 5 15:28:02 CEST 2000


On Fri, 5 May 2000 15:05:09 +0200 (MEST), rei05 at gmx.de <rei05 at gmx.de> wrote:
>hi there,
>i have a string s and i need to replace the substring "'" (i.e. the
>symbol with ascii 146) with the substring "\'";  so i tried
>string.replace(s,"'","\'"), but this doesn`t work. anyone any
>better idea?
>thanx, rei
>

Well, there are two possible problems :

1. Try printing the string "\'" -- I don't think it's
   what you want it to be.

   Use r"\'" or "\\'" instead.

2. Strings are immutable, so s itself is not changed.

So all together,
   new_s = string.replace(s, "'", r"\'")
should do the trick.

[I'm not sure if ' is the actual symbol you're interested in,
since that's ascii 39 for me, but this should work with whatever
you want in place of '.]

Jeff Raven



More information about the Python-list mailing list