Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Your example isn't very compelling, but for longer strings with more complex formatting, I'd say .format() is pretty compelling. I find that in general

    '{x} blah blah blah {y}'.format(x=x, y=y)
is more readable than

    '%s blah blah blah %s' % (x, y)
even if the former is a bit longer.

On top of that, there's a whole bunch of stuff you can do with .format() that just isn't possible with %.



Even .format() is quite redundant for the "standard" usage, as we had string interpolation using '%' with dicts for a long time.

    '%s blah blah blah %s' % (x, y)
    '{x} blah blah blah {y}'.format(x=x, y=y)
    '%(x)s blah blah blah %(y)s'%{'y':y, 'x':x}




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: