[Tutor] FW: wierd replace problem

Steven D'Aprano steve at pearwood.info
Tue Sep 14 23:37:06 CEST 2010

On Wed, 15 Sep 2010 07:20:33 am Walter Prins wrote:
> Correction on my last paragraph on my last mail:
> "See also when Python is asked to "print" the string, you can see the
> escape characters really there." -> "See also when Python is asked to
> "print" the string, you can see the escape characters aren't part of
> the actual contents of the string."

I think that half of the confusion here is that people are confused 
about what escape characters are. When you read text from a file, and 
Python sees a backslash, it DOESN'T add a second backslash to escape 
it. Nor does it add quotation marks at the start or end.

Text is text. The characters you read from a text file -- or the bytes 
you read from any file -- remain untouched, exactly as they existed in 
the file. But what changes is the DISPLAY of the text.

When you have the four characters abcd (with no quotation marks) and you 
ask Python to display it on the command line, the display includes 
punctuation, in this case quotation marks, just like a list includes 
punctuation [,] and a dict {:,}. If the string includes certain special 
characters like newlines, tabs, backslashes and quotation marks, the 
display uses escape codes \n \t \\ \' or \" as punctuation to the 
display only. But the extra backslashes don't exist in the string, any 
more than lists include items [ and ].

When you enter string literals, the way to enter them is by typing the 
display form. The display form includes matching quotation marks at the 
beginning and end, and escaping special characters. But that 
punctuation isn't part of the string.

Steven D'Aprano

More information about the Tutor mailing list