[CentralOH] Python % Formatting

Catherine Devlin catherine.devlin at gmail.com
Thu Dec 6 23:01:52 CET 2007


Hmm, neat question, and good answers so far.

I usually end up just doing something like

'%s %s%s' % (firstName, ((middleName or '') and '%s ' % middleName), lastName)

or, with the Python 2.5 syntax,

'%s %s%s' % (firstName, (('%s ' % middleName) if middleName else ''), lastName)

... but, of course, neither of those is completely pretty.

There are a bunch of templating engines that are great when the basic
% interpolation just isn't enough.  Cheetah is the one that comes to
mind.  I don't know it that well, so I can't actually say if there's a
solution in there for you, but it's a good thing to try if built-in
substitution isn't enough.

As you can see, there's more than one way to do it!  (Oops, wash my mouth out.)
-- 
- Catherine
http://catherinedevlin.blogspot.com/


More information about the CentralOH mailing list