I've searched for this, but I'm not sure how to word what I'm asking, so it's difficult to find whether somebody has already posted a solution.
I'd like to format some variables into a descriptive string. As an example:
'My name is {name}. I like to eat {food}.'.format(name='Ben', food='pizza')
gives (obviously): 'My name is Ben. I like to eat pizza.'
But I'd like to omit the entire second sentence in the case that food is None or '' (empty). So:
'My name is {name}. I like to eat {food}.'.format(name='Ben', food='')
gives: 'My name is Ben.'
Is there something I can wrap around the second sentence to make it conditional on there being a non-blank value of food? I can do this in an inelegant way by joining strings and suchlike, but I was looking for a more Pythonic solution. Perhaps I need to subclass Formatter, but I'm not sure where to start with that.
(As an aside, I know that Winamp used to be able to do this in format strings depending on whether tags were present or not in an MP3 file.)
 
    