Conversion to string: how do `s work?

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Mon Mar 14 17:21:11 EST 2005


Chris Lasher wrote:

> I'm working my way through _Learning_Python_ 2nd ed., and I saw
> something peculiar which is not explained anywhere in the text.
> 
> print " " + file + " size=" + `size`
> 
> The `s appear to somehow automagically convert the integer to a string
> for concatenation. How does this work? Is this just a shortcut for
> str(size)? Is it considered bad practice to use `s?

In addition to the fact that `` is repr() and generally shouldn't be
used, the above is much better written using string formatting:

    print ' %s size=%u' % (file, size)

Shorter, cleaner, and more explicit.

Tim Delaney



More information about the Python-list mailing list