Escapeism

Duncan Booth duncan.booth at invalid.invalid
Sat Sep 30 13:23:52 EDT 2006


"Kay Schluehr" <kay.schluehr at gmx.net> wrote:

>> try "print repr(c)".
> 
> This yields the hexadecimal representation of the ASCII character and
> does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
> escape semantics.

But you yourself noted earlier that '\a' and '\x07' are the same string
and said: 

> But lets weaken the requirement and fix a canonical representation in
> case of ambiguity.

That's exactly what repr(c) does, it uses a canonical representation
with '\t', '\r', '\n', '\\', (and when it has to "\'", '\"') using the 
short escape form (because they are so commonly used), and the all the 
other (more obscure) escape sequences using the hexadecimal form.

BTW, c.encode('string_escape') is another way to convert a string to almost 
the same escaped form (except for a minor difference in the treatment of 
quote characters).

> But unfortunately "rawrification" applies only to string literals and
> not to string objects ( such as c ).

Oh dear, the fact that you could even consider writing that sentence seems 
to show a fundamental misunderstanding of what a raw string literal means.



More information about the Python-list mailing list