In an attempt to answer this question, I managed to get the string to print the escape characters by escaping the backslash.
When I try to generalize it to escape all escaped characters, it seems to do nothing:
>>> a = "word\nanother word\n\tthird word" >>> a 'word\nanother word\n\tthird word' >>> print a word another word third word >>> b = a.replace("\\", "\\\\") >>> b 'word\nanother word\n\tthird word' >>> print b word another word third word but this same method for specific escape characters, it does work:
>>> b = a.replace('\n', '\\n') >>> print b word\nanother word\n third word >>> b 'word\\nanother word\\n\tthird word' Is there a general way to achieve this? Should include \n, \t, \r, etc.
\nto specify a newline, but Python converts that to a single, literal linefeed character in the resultingstrobject.