1

Suppose I have the following string in Python:

>>> example=""" ... \nthird line ... [\t] <-tab in there ... [\n] <-\\n in there ... \v vtab ... 1\b2 should be only '2' ... this\rthat <- should be only 'that' ... """ 

If I print that, the various escaped characters (like \t for a tab) are interpolated into a human readable form:

>>> print example third line [ ] <-tab in there [ ] <-\n in there vtab 2 should be only '2' that <- should be only 'that' 

What if I want to only produce a string with the various escape codes expanded or interpreted without printing it? Somthing like:

>>> exp_example = example.expandomethod() 

(I have looked at the various string methods, decode, and format but none are working as in this example.)


Edit

OK -- Thanks for the help for the thick scull on my part. I was convinced that these strings were being parsed, which they are, but it the display of them that was fooling me.

I worked this out in my own mind:

>>> cr='\012' # CR or \n in octal >>> len(cr) 1 >>> '123'+cr '123\n' >>> '123\012' == '123\n' True 
7
  • @Amadan: But what if I want the string with the escape codes interpreted as if printed? I could do a stringIO thingy I suppose, but that seems so unpythonically indirect... Commented Dec 10, 2012 at 2:26
  • StringIO will preserve every byte you send it. It will not redact contents and act as if it was a terminal. Commented Dec 10, 2012 at 2:28
  • print >> tempfile and read it back again? Commented Dec 10, 2012 at 2:36
  • 2
    Can you give an example of what you expect the resulting string to be? Commented Dec 10, 2012 at 2:37
  • 1
    @drewk: As others have pointed out, the string "\t\n\t" isn't six characters long, it's three. You type six characters in your source code but there are only three in the resulting string. The resulting string is the three-byte string you say you want. Commented Dec 10, 2012 at 5:48

4 Answers 4

1

They are not interpolated. They are printed. For example, \t will normally print a number of spaces; this\rthat will print this, go back and print that over it. If you were to print it on a printer, you'd see both words.

If you wanted to reduce a string to a print-equivalent string, I suppose you would have to write your own terminal emulator - I am not aware of any library to do it for you.

A better question is - why do you need it? It looks very much like an XY problem.

Sign up to request clarification or add additional context in comments.

Comments

1

There are some characters whose representation is different from what they look like when printed. (Newline '\n' is just the most obvious one.) You can't really store the way these characters look when printed. It would be like asking how to store the way a particular font makes a character look.

>>> example="""a ... b""" >>> print example # This is what a newline looks like. You cannot capture it. a b >>> example # This is how a newline is represented. 'a\nb' 

Comments

1

print doesn't interpret anything. It's already the string itself which has differing internal and external representation.

Proof:

s = "\t" len(s) 

...yields 1 and not 2

Comments

0

As others have stated, when you type in your escaped string, or Python first interprets the string, the escape character \ and the character following are reduced to the single targeted character.

However -- if you are constructing a string that with to goal of producing unprintable characters from their escape sequence, str.decode([encoding[, errors]]) does what you want:

>>> s='string' >>> esc='\\' >>> n='n' >>> st=s+esc+n+'next line' >>> print st string\nnextline >>> print st.decode('string_escape') string next line 

And this:

>>> ''.join(['\\','n','\\','t'])=='\n\t' False 

is a different result than this:

>>> ''.join(['\\','n','\\','t']).decode('string_escape')=='\n\t' True 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.