Conditionally including additional characters using python string formatting

python

Solution

Why don't you use strip():

>>> format_string = "{last}, {first}"
>>> name = {'first': 'John', 'last':''}
>>> format_string.format(**name).strip(', ')
>>> 'John'

Problem

Is it possible using python's string format to conditionally include additional characters along with the string variable only if the variable is not empty? ``` >>> format_string = "{last}, {first}" >>> name = {'first': 'John', 'last':'Smith'} >>> format_string.format(**name) 'Smith, John' # great! >>> name = {'first': 'John', 'last':''} >>> format_string.format(**name) ', John' # don't want the comma and space here, just 'John' ``` I would like to use the same`format_string` variable to handle any combination of empty or non-empty values for `first` or `last` in the `name` dict. What's the easiest way to do this in python?

Original source