nummertolv wrote: > - Consider a string variable containing backslashes. > - One or more of the backslashes are followed by one of the letters > a,b,f,v or a number. > > myString = "bar\foo\12foobar" > > How do I print this string so that the output is as below? > > bar\foo\12foobar > > typing 'print myString' prints the following: > > baroo > foobar > > and typing print repr(myString) prints this: > > 'bar\x0coo\nfoobar' >
The interpretation of escape sequences happens when the Python compiler reads the string "bar\foo\12foobar". You'll see that when you do something like >>> map (ord, "bar\foo\12foobar") [98, 97, 114, 12, 111, 111, 10, 102, 111, 111, 98, 97, 114] This displays the ASCII values of all the characters. If you want to use a string literal containing backslashes, use r'' strings: >>> myString = r'bar\foo\12foobar' >>> map (ord, myString) [98, 97, 114, 92, 102, 111, 111, 92, 49, 50, 102, 111, 111, 98, 97, 114] >>> print myString bar\foo\12foobar >>> print repr (myString) 'bar\\foo\\12foobar' If you get the strings from an external source as suggested by your original post, then you really have no problem at all. No interpretation of escape sequences takes place when you read a string from a file. Daniel -- http://mail.python.org/mailman/listinfo/python-list