Illegal literal or not?

Beni Cherniavsky cben at techunix.technion.ac.il
Sun Jan 26 14:26:42 CET 2003


On 2003-01-24, LJ & MT Lareau wrote:

> Is it possible to have Python produce the following literal output in the
> interactive mode.
> '"Let's try this!", they said.'
> If so what is the Python statement.  I've tried several ways
> and can't seem to produce it.  Thanks for the help.
>
Instead of printing, you can just use the fact that a string literal (as
any expression) typed at the interactive prompt will print its repr.
This is very simple and almost gets there:

>>> '"Let\'s try this!", they said.'
'"Let\'s try this!", they said.'

Now let's do something the other replies didn't <wink>: create a class.

>>> class rawstr(str):
...   def __repr__(self):  # Yes, I know unevalable reprs are bad.
...     return str.__repr__(self).replace('\\','')
...
>>> rawstr('"Let\'s try this!", they said.')
'"Let's try this!", they said.'

Just-my-`float("""0"""''"""."""''"""0"""''"""2""")`-shekels-ly y'rs,
    Beni Cherniavsky <cben at tx.technion.ac.il>





More information about the Python-list mailing list