append special chars with "\"
Gerrit Holl
gerrit at nl.linux.org
Tue Sep 30 07:33:31 EDT 2003
tertius wrote:
> 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
Another possibility:
for c in chr:
str = str.replace(c, r"\c")
Untested, however, but something like this should work.
Gerrit.
--
1. If any one ensnare another, putting a ban upon him, but he can not
prove it, then he that ensnared him shall be put to death.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
http://www.sp.nl/
More information about the Python-list
mailing list