Simple Problem

Robert Kern rkern at ucsd.edu
Sun Jul 24 21:31:39 EDT 2005


ncf wrote:
> I know I've seen this somewhere before, but does anyone know what the
> function to escape a string is? (i.e., encoding newline to "\n" and a
> chr(254) to "\xfe") (and visa-versa)

In [1]: s = "foo\n\xfe"

In [2]: s.encode("string_escape")
Out[2]: 'foo\\n\\xfe'

In [3]: repr(s)[1:-1]
Out[3]: 'foo\\n\\xfe'

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list