%r
Peter Hansen
peter at engcorp.com
Mon Mar 6 12:00:37 EST 2006
Blackbird wrote:
> I'm trying to get a complete grip on %r. Is it true that the two programs
>
> a = '*anything the parser accepts*'
> print '%r' % a
>
> vs.
>
> a = r'*anything the parser accepts*'
> print "'%s'" % a
>
> always produce the same output, where *anything the parser accepts* can be
> replaced with, well, anything the parser accepts?
"Always produce the same output?" Well, hardly, as even a token test
proves:
>>> a = 'testing\'this'
>>> print '%r' % a
"testing'this"
>>> b = r'testing\'this'
>>> print "'%s'" % b
'testing\'this'
Do you realize that '%r' % a just outputs whatever repr(a) returns? And
that '%s' % a just outputs whatever str(a) returns?
-Peter
More information about the Python-list
mailing list