append special chars with "\"

Mark Hahn mark at hahnca.com
Tue Sep 30 18:32:58 EDT 2003


Isn't this what re.escape(string) is for?

"tertius" <tcronj at ananzi.co.za> wrote in message
news:3f793544$0$64721 at hades.is.co.za...
> Is there a better way to append certain chars in a string with a
> backslash that the example below?
>
> chr = "#$%^&_{}"          # special chars to look out for
> str = "123 45^ & 00 0_"   # string to convert
> n = ""                    # init new string
> for i in str:
>      if i in chr:          # if special character in str
>          n+='\\'           # append it with a backslash
>      n+=i
> print "old:",str
> print "new:",n
>
>
> Thanks
> Tertius
>






More information about the Python-list mailing list