dawg
dawg

Reputation: 104062

Interpret escaped strings like print does

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

Upvotes: 1

Views: 652

Answers (4)

the wolf
the wolf

Reputation: 35562

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

Upvotes: 0

Jo So
Jo So

Reputation: 26531

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

Upvotes: 1

kojiro
kojiro

Reputation: 77157

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'

Upvotes: 1

Amadan
Amadan

Reputation: 198446

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.

Upvotes: 1

Related Questions