Illegal literal or not?
Bengt Richter
bokr at oz.net
Sun Jan 26 15:20:30 EST 2003
On Sun, 26 Jan 2003 15:26:42 +0200, Beni Cherniavsky <cben at techunix.technion.ac.il> wrote:
>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>
>
Try this with that:
>>> print r"""
...
... >>> rawstr('"Let\'s try this!", they said.')
... '"Let's try this!", they said.'
...
... """[2:-2]
>>> rawstr('"Let\'s try this!", they said.')
'"Let's try this!", they said.'
>>>
I.e.,
>>> rawstr(r"""
...
... >>>> rawstr('"Let\'s try this!", they said.')
... >'"Let's try this!", they said.'
...
... """[2:-2])
'>>>> rawstr('"Let's try this!", they said.')n>'"Let's try this!", they said.''
>>>
Reminds me of old threads on the quest for a wartless (i.e., can quote any sequence
of printables including any instance of its own use) quoting mechanism ;-)
(need dynamically defined delimiting)
Regards,
Bengt Richter
More information about the Python-list
mailing list