print string as raw string
Diez B. Roggisch
deets at nospam.web.de
Tue Feb 17 05:39:48 EST 2009
Mirko Dziadzka schrieb:
> Hi all
>
> I'm trying to find a way to output strings in the raw-string format, e.g.
>
> print_as_raw_string(r"\.") should output r"\." instead of "\\."
>
> Is there a better way than writing your own print function? Some magic
> encoding?
There is no need to do this. Rawstrings are only different wrt to their
parsing, the resulting string is the exact same.
r"\foo" == "\\foo"
type(r"\foo") == type("\\foo")
So there is no way to write something that distinguishes them.
Can it be that you are fooled by the repr() of strings in the interpreter?
>>> r"\foo"
'\\foo'
This is nothing to worry about, it's just the interpreter doing an
implicit repr-call for the objects it displays.
If instead you'd do
>>> print r"\foo"
\foo
you get what you want.
Diez
More information about the Python-list
mailing list