Unrecognized escape sequences in string literals

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Mon Aug 10 08:10:53 CEST 2009

On Sun, 09 Aug 2009 18:34:14 -0700, Carl Banks wrote:

>> Why should a backslash in a string literal be an error?
> Because the behavior of \ in a string is context-dependent, which means
> a reader can't know if \ is a literal character or escape character
> without knowing the context, and it means an innocuous change in context
> can cause a rather significant change in \.

*Any* change in context is significant with escapes.

"this \nhas two lines"

If you change the \n to a \t you get a significant difference. If you 
change the \n to a \y you get a significant difference. Why is the first 
one acceptable but the second not?

> IOW it's an error-prone mess.

I've never had any errors caused by this. I've never seen anyone write to 
this newsgroup confused over escape behaviour, or asking for help with an 
error caused by it, and until this thread, never seen anyone complain 
about it either.

Excuse my cynicism, but I believe that you are using "error-prone" to 
mean "I don't like this behaviour" rather than "it causes lots of errors".


More information about the Python-list mailing list